我有一个问题是为角度中的ui-select2预选一组变量。
我的控制器:
$scope.roles= [{"_id":"545e8f2f64f29dc1540f5a88","__v":0,"created":"2014-11-08T21:46:23.553Z","comments":"Administrator Rolle","name":"admin"}];
$scope.userdata.roles = [{"_id":"545e8f2f64f29dc1540f5a88","__v":0,"created":"2014-11-08T21:46:23.553Z","comments":"Administrator Rolle","name":"admin"}];
我的观点:
<ui-select multiple ng-model="userdata.roles" theme="select2" ng-disabled="disabled" class="input-medium" style="width:100% !important;">
<ui-select-match placeholder="Wählen Sie ein oder mehrere Rollen...">{{$item.name}}</ui-select-match>
<ui-select-choices repeat="role in roles | filter:$select.search">
{{role.name}} ({{role.comments}})
</ui-select-choices>
</ui-select>
ng-model不要预先选择值
当我分配$scope.userdata.roles = $scope.roles
它将起作用。
如果可能,我想使用这个数组,但我无法用这个代码编写:
$scope.roles= [{"_id":"545e8f2f64f29dc1540f5a88","__v":0,"created":"2014-11-08T21:46:23.553Z","comments":"Administrator Rolle","name":"admin"}];
$scope.userdata.roles = ["admin"];
编辑: 当我使用它作为我的观点时:
<ui-select multiple ng-model="userdata.roles" theme="select2" ng-disabled="disabled" style="width:100% !important;">
<ui-select-match placeholder="Bitte wählen Sie eine oder mehrere Rollen...">{{$item.name}} <{{$item.comments}}></ui-select-match>
<ui-select-choices repeat="role.name as role in roles">
<div ng-bind-html="role.name | highlight: $select.search"></div>
<small>
{{role.comments}}
</small>
</ui-select-choices>
</ui-select>
这是我的控制器:
$scope.roles = [
{
"_id" : "545e8f2f64f29dc1540f5a88",
"__v" : 0,
"created" : "2014-11-08T21:46:23.553Z",
"comments" : "Administrator Rolle",
"name" : "admin"
}
];
$scope.userdata.roles = ["admin"];
它也行不通。时机有问题吗?
答案 0 :(得分:0)
您需要从数组中选择要设置为默认值的整个对象,如
$scope.roles= [{"_id":"545e8f2f64f29dc1540f5a88","__v":0,"created":"2014-11-08T21:46:23.553Z","comments":"Administrator Rolle","name":"admin"}];
$scope.userdata.roles = $scope.roles[0] //set first record as default;
答案 1 :(得分:0)
关键是将$ parent添加到我的ngmodel var。