【发布时间】:2014-03-08 01:23:12
【问题描述】:
即使对象相等,选择字段中的预选也不起作用:
<select ng-show="isEditMode(todo.id)" id="assignee" name="assignee"
ng-model="todo.assignee" required
ng-options="user.name for user in users">
</select>
todo.assignee 包含一个用户对象,它应该与用户中的一个匹配。
Angular 似乎无法识别来自 todo.assignee 的 User 对象包含在 users 中。我可以手动执行此映射吗?
选择显示为未选择任何值。我可以(从用户中)选择一个用户并毫无问题地保存记录。
控制器
$scope.todos = Todo.query();
$scope.users = User.query();
更新
根据 cmets 的要求。给定对象的结构: $scope.todos
[
{
"id": 157,
"description": "my description 0",
"deadline": 1392073200000,
"assignee": {
"id": 34,
"name": "User 1",
"email": "user1@hotmail.com"
},
"comment": "my comment 0",
"done": true
}
...
]
$scope.users
[
{
"id": 34,
"name": "User 1",
"email": "user1@hotmail.com"
},
{
"id": 35,
"name": "User 2",
"email": "xxc@gmail.com"
},
{
"id": 36,
"name": "User 3",
"email": "xx@hotmail.com"
}
]
select的范围来自于一个repeat:
<tr ng-repeat="todo in todos | filter:query | filter:{assignee:queryAssignee} | filter:queryDone" ng-class="{danger: isDue(todo)}">
<td>
【问题讨论】:
-
能否添加
users和todos的结构?如果todo.assignee包含用户对象,您可以使用类似:ng-model="todo.assignee.user.name" -
你必须在你的 ng-model 和 ng-show 上做 todo,但在你的范围上做 todos。
-
我已按要求添加了结构。还提供了为什么 todo 在我的范围内的更多信息。
标签: javascript angularjs