使用angularjs中的ng-options从选择框中选择多个选项

时间:2014-05-19 06:16:45

标签: javascript angularjs

假设我的html看起来像:

<div>
<select  class="topcoat-select"  ng-model="myselect" ng-options="o.cost as (o.attr_value+'-$'+' '+o.cost) group by o.attribute_text for o in options" ng-change="itemchange()" >  
    <option  value="">--Select Options--</option>
</select>
</div>

现在我只能选择单个选项,并且我在ng-model中获得该单个选项的值。但我想选择多个选项,我想获得ng-model中所选选项的值。

是否可以从angularjs中的单个选择框中选择多个选项?

1 个答案:

答案 0 :(得分:0)

您可以在multiple="multiple"元素声明中添加select。然后,ng-model中的所选选项将是逗号分隔的值数组。

<select ng-options="record.id as record.value for record in records" ng-model="output" multiple="multiple"></select>

<button ng-click="alertValues()">Click Me!</button>

$scope.output = "";

$scope.records = [{
    id: 1,
    value: "One",
}, {
    id: 2,
    value: "Two",
}, {
    id: 3,
    value: "Three",
}];   

$scope.alertValues = function(){        
    alert($scope.output);        
}   

http://jsfiddle.net/DianaNassar/6W8Xg/1/