如何设置一个默认值NG-选项(多选) [英] How to set a default value to ng-options (multiple select)

查看:162
本文介绍了如何设置一个默认值NG-选项(多选)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过设置一个默认值,我的NG选项 NG-INIT NG-模型等他们并没有在所有的工作。我的code为以下内容:

I wanted to set a default value to my ng-options by using ng-init, ng-model, etc. They didn't work at all. My code is following:

var app = angular.module('role', []);

app.controller('fooController', function($scope){
    $scope.roles = [{id:1, name:"Administrator"}, {id:2, name: "Student"}];
    $scope.user.roles = $scope.roles[0];
});

HTML

<body data-ng-app="role" data-ng-controller="fooController">
  <select multiple class="form-control" data-ng-model="user.roles" data-ng-options="role.name for role in roles" required=""></select>
</body>

下面是我的 Plunkr。
任何帮助将是AP preciated。

Here is my Plunkr. Any help would be appreciated.

推荐答案

下面是我工作:

app.controller('fooController', function($scope){
    $scope.roles = [{id:1, name:"Administrator"}, {id:2, name: "Student"}];
    $scope.user = {};
    $scope.user.roles = [ $scope.roles[0] ];
});

有没有在这普拉克用户未初始化错误,除了这个,角将在中的每个对象比较NG选项数组中的每个元素在 NG-模型阵列。 JavaScript的比较没有棱角比较,比较,即使相同属性的两个对象在JavaScript中返回false(不同的对象)。

There was an error in the plunk that user wasn't initialized, apart from this, Angular would compare every object in the ng-options array to every element in the ng-model array. JavaScript comparison not Angular comparison, comparing 2 objects even with same properties in JavaScript returns false (different objects).

普拉克:<一href=\"http://plnkr.co/edit/ccsAVvPACnN6Qzje7HcB?p=$p$pview\">http://plnkr.co/edit/ccsAVvPACnN6Qzje7HcB?p=$p$pview

现在,这是不理想的。通常情况下,你要根据ID或因此这些相关的,这里的另一种方式:

Now, this is not ideal. Typically you want to correlated these based on ID or so, here's another way:

在HTML中,使用通过跟踪,告诉AngularJS比较的ID,而不是整个对象:

In HTML, use track by, to tell AngularJS to compare IDs instead of entire objects:

  <select multiple class="form-control" 
    data-ng-model="user.roles" 
    data-ng-options="role.name for role in roles track by role.id" 
    required=""></select>

然后在你的控制器,你可以使用任何对象,而无需在阵列中居然是:

Then in your controller, you can use any object without actually being in the array:

app.controller('fooController', function($scope){
    $scope.roles = [{id:1, name:"Administrator"}, {id:2, name: "Student"}];
    $scope.user = {};
    $scope.user.roles = [ {id:1, name:"Administrator"} ];
});

普拉克:<一href=\"http://plnkr.co/edit/NKLXrwqwk36YfhBSXILN?p=$p$pview\">http://plnkr.co/edit/NKLXrwqwk36YfhBSXILN?p=$p$pview

请注意,我甚至没有需要名称属性。这只是供以后作出适当的对象,但它确实是没有必要现在匹配,尝试没有它!

Note that I didn't even need the name property. It's just for making a proper object later, but it really isn't needed for matching now, try without it!

我写了一个很长的教程和视频在这个最初的选择问题。它不包括多选,但是这只是使用一个数组,而不是直接的对象。检查出来的更多的理解:

I wrote a long tutorial and video on this initial selection problem. It doesn't cover multiselect but that's just using an array instead of the object directly. Check it out for more understanding:

<一个href=\"http://gurustop.net/blog/2014/01/28/common-problems-and-solutions-when-using-select-elements-with-angular-js-ng-options-initial-selection/\">Solving最初的选择问题在Angular.JS选择,与轨道NG选项由

让我知道意见,如果你还在为此苦苦挣扎。

Let me know in comments if you are still struggling with this.

这篇关于如何设置一个默认值NG-选项(多选)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆