2013-11-26 106 views
5

我试图预选我的多选选项。为了明确我的情况,我做了一个JSFiddleAngularJS:多选择预选选项

<select ng-model="properties" id="props" multiple 
ng-options="option.name group by option.category for option in options"></select> 

不幸的是,我受到对象的接收方式的约束,所以我想我需要ng-options属性。

有没有人有一个想法我怎么能得到'美国队长'和'博士厄运'选择负载?

回答

2

你超级接近,两个变化。定义你的控制器属性这样

$scope.properties = ["Captain America", "Dr. Doom"]; 

一小片添加到您的NG-选择这样

ng-options="option.name as option.name group by option.category for option in options" 

你option.name这将确定保存功能看起来像在$范围.properties

+0

你也可以做$ scope.properties = [“4”,“9”]并使用option.id作为option.name –

3

试着改变控制器代码:

function TestCtrl($scope) { 

    var myOptions = [{ 
     "id": "4", 
     "name": "Captain America", 
     "categoryId": "1", 
     "category": "Heroes" 
    }, { 
     "id": "5", 
     "name": "Iron Man", 
     "categoryId": "1", 
     "category": "Heroes" 
    }, { 
     "id": "10", 
     "name": "Magneto", 
     "categoryId": "2", 
     "category": "Vilains" 
    }, { 
     "id": "9", 
     "name": "Dr. Doom", 
     "categoryId": "2", 
     "category": "Vilains" 
    }]; 

    $scope.options = myOptions; 


    $scope.properties = [myOptions[0], myOptions[3]]; 
} 

说明:您所选的选项(属性)设置为比组成的完整列表的那些对象的不同实例。使用与上面所示相同的对象引用。

+0

是的,我试过这个解决方案 - http://jsfiddle.net/k85M4/ – Eugene