0

我在为 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}} &lt;{{$item.comments}}&gt;</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"];

它也行不通。时间有问题吗?

4

2 回答 2

0

关键是将 $parant 添加到我的 ng-model 变量中。

于 2014-11-10T13:25:57.433 回答
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;
于 2014-11-10T12:51:52.870 回答