选择的 ng-options 不使用对象作为模型 [Angular 1.5]
ng-options selected not working with object as model [Angular 1.5]
下面的代码和一切加载正常,除了它不是 select 默认选项。
此外,option.groupJID
的下拉项值之一与 MyCtrl.groupJID = 1655153520848838061@groups.go.to
匹配
<select id="groups-list2" ng-model="MyCtrl.groupJID"
ng-options="option.groupJID as option.profile.name for option in MyCtrl.userGroups track by option.groupJID">
</select>
请评论
函数:
SomeNetworkCall.then(function(success){
MyCtrl.groupJID = success.peer; //'1655153520848838061@groups.go.to'
MyCtrl.userGroups = success.groups;
});
success.groups:
[
{
"groupJID": "11110567@groups.go.to",
"profile": {
"name": "Flock Desktop Dev - QA issues",
"description": "QA/Dev Channel"
}
},
{
"groupJID": "1655153520848838061@groups.go.to",
"profile": {
"name": "Some group name",
"description": "awesome Channel"
}
}
{
"groupJID": "1111067890@groups.go.to",
"profile": {
"name": "Flock Desktop 1",
"description": "QA/Dev Channel"
}
},
{
"groupJID": "1111045678@groups.go.to",
"profile": {
"name": "Flock Desktop 2",
"description": "QA/Dev Channel"
}
}
]
取自 docs
Be careful when using select as and track by in the same expression.
您在 Select 中指定的值作为 将用作选项值,但是当您添加 Track By 表达式将被跟踪的值是 Select as 值而不是对象。
在您的情况下,ngModel 值为 option.groupJID
并且 track by expression 的计算结果为 option.groupJID.groupJID
,这是未定义的。
要使其正常工作,您可以删除 Track by expression
<select id="groups-list2" ng-model="MyCtrl.groupJID"
ng-options="option.groupJID as option.profile.name for option in MyCtrl.userGroups">
</select>
笨蛋 here
下面的代码和一切加载正常,除了它不是 select 默认选项。
此外,option.groupJID
的下拉项值之一与 MyCtrl.groupJID = 1655153520848838061@groups.go.to
<select id="groups-list2" ng-model="MyCtrl.groupJID"
ng-options="option.groupJID as option.profile.name for option in MyCtrl.userGroups track by option.groupJID">
</select>
请评论
函数:
SomeNetworkCall.then(function(success){
MyCtrl.groupJID = success.peer; //'1655153520848838061@groups.go.to'
MyCtrl.userGroups = success.groups;
});
success.groups:
[
{
"groupJID": "11110567@groups.go.to",
"profile": {
"name": "Flock Desktop Dev - QA issues",
"description": "QA/Dev Channel"
}
},
{
"groupJID": "1655153520848838061@groups.go.to",
"profile": {
"name": "Some group name",
"description": "awesome Channel"
}
}
{
"groupJID": "1111067890@groups.go.to",
"profile": {
"name": "Flock Desktop 1",
"description": "QA/Dev Channel"
}
},
{
"groupJID": "1111045678@groups.go.to",
"profile": {
"name": "Flock Desktop 2",
"description": "QA/Dev Channel"
}
}
]
取自 docs
Be careful when using select as and track by in the same expression.
您在 Select 中指定的值作为 将用作选项值,但是当您添加 Track By 表达式将被跟踪的值是 Select as 值而不是对象。
在您的情况下,ngModel 值为 option.groupJID
并且 track by expression 的计算结果为 option.groupJID.groupJID
,这是未定义的。
要使其正常工作,您可以删除 Track by expression
<select id="groups-list2" ng-model="MyCtrl.groupJID"
ng-options="option.groupJID as option.profile.name for option in MyCtrl.userGroups">
</select>
笨蛋 here