我在angular中为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"];它也不会起作用。时间上有问题吗?
发布于 2014-11-10 20:51:53
您需要从数组中选择要设置为默认对象的整个对象,如
$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;发布于 2014-11-10 21:25:57
关键是将$parant添加到我的ng-model变量中。
https://stackoverflow.com/questions/26844027
复制相似问题