Javascript AngularJS选择预选不工作
即使对象相等,预选在“选择”字段中也不起作用:Javascript AngularJS选择预选不工作,javascript,angularjs,Javascript,Angularjs,即使对象相等,预选在“选择”字段中也不起作用: <select ng-show="isEditMode(todo.id)" id="assignee" name="assignee" ng-model="todo.assignee" required ng-options="user.name for user in users"> </select> 更新 按照评论中的要求。给定对象的结构: $scope.todos [ {
<select ng-show="isEditMode(todo.id)" id="assignee" name="assignee"
ng-model="todo.assignee" required
ng-options="user.name for user in users">
</select>
更新
按照评论中的要求。给定对象的结构:
$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
}
...
]
[
{
"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"
}
]
$scope.users
[
{
"id": 157,
"description": "my description 0",
"deadline": 1392073200000,
"assignee": {
"id": 34,
"name": "User 1",
"email": "user1@hotmail.com"
},
"comment": "my comment 0",
"done": true
}
...
]
[
{
"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"
}
]
选择的范围来自重复:
<tr ng-repeat="todo in todos | filter:query | filter:{assignee:queryAssignee} | filter:queryDone" ng-class="{danger: isDue(todo)}">
<td>
根据您的描述: 包含一个用户对象 但是选项的值是
user.name
字符串,一个对象和一个字符串将永远不会匹配
那么,替换
ng-model="todo.assignee"
到
更新:
使用ng options=“user.name as user.name for user in users”
完整答案:
<select ng-show="isEditMode(todo.id)"
ng-model="todo.assignee.name" required
ng-options="user.name as user.name for user in users">
</select>
但是,现在:
When you select one of option, it assign object value to model todo.assignee, so let what you want.
todo.assignee.name = {
"id": 36,
"name": "User 3",
"email": "user3@hotmail.com"
} // like this
todo.assignee // now change the whole value
/* {"id": 36,
"name": "User 3",
"email": "user3@hotmail.com"} */
也许它对其他人有用:
<select ng-show="isEditMode(todo.id)" id="assignee" name="assignee"
ng-model="todo.assignee" required
ng-options="user as user.name for user in users track by user.id">
</select>
魔术在于“跟踪用户id”
能否添加
用户
和待办事项
的结构?如果todo.assignee
包含用户对象,您可以使用类似以下内容:ng model=“todo.assignee.user.name”
您的ng模型上有todo,而ng show上有todo,但范围上有todo。我已按要求添加了结构。还提供了更多关于为什么todo在我的范围内的信息。这并不能解决问题。我试过了。我还在select之后添加了{{todo.assignee.name}}。它正确地显示了名字。非常感谢你的帮助。预选现在起作用了。但它也有副作用。它将创建无效的对象。我希望todo.assignee中的完整用户对象会被更改,但只有名称会像这样更改。所以我需要在后台解决这个问题(根据名称设置正确的对象),这有点难看。请再次更新我的答案~恐怕这会导致另一个问题。值现在是字符串,不再是对象。因此,我更喜欢选择后处理的解决方法。我认为我的情况不应该很少发生,所以我很惊讶angular不能处理这个问题。我补充了一些解释。不要害怕。这并不可怕,这是常见的解决办法。你可以参考这个:这对我有用!需要注意的是,模型必须是对对象的引用,而不是对todo.assignment.id的引用。
When you select one of option, it assign object value to model todo.assignee, so let what you want.
todo.assignee.name = {
"id": 36,
"name": "User 3",
"email": "user3@hotmail.com"
} // like this
todo.assignee // now change the whole value
/* {"id": 36,
"name": "User 3",
"email": "user3@hotmail.com"} */
<select ng-show="isEditMode(todo.id)" id="assignee" name="assignee"
ng-model="todo.assignee" required
ng-options="user as user.name for user in users track by user.id">
</select>