我怎么能在角材料中实现多个选择选项?

时间:2015-02-22 08:28:33

标签: angularjs material-design angular-material

我已经检查了文档和演示,但唉!我没有找到任何使用angular-material实现多选项的参考,例如select 2。 有谁能告诉我如何让它发挥作用?

5 个答案:

答案 0 :(得分:25)

Splaktar的回答是正确的:只需将多个添加到 md-select 即可。

工作解决方案的Codepen:http://codepen.io/ansgar-john/pen/RWPVEO?editors=101

HTML

<div>
  <md-input-container>
    <md-select multiple ng-model="ctrl.likedAnimals" placeholder="please select">
      <md-option ng-repeat="a in ctrl.animals" value="{{a}}">{{a}}</md-option>
    </md-select>
  </md-input-container>
</div>

JS

(function () {
  'use strict';
  angular
      .module('MyApp')
      .controller('AppCtrl', function($scope) {
        this.likedAnimals = ["mouse", "dog"];
        this.animals = ["mouse", "dog", "cat", "bird"];
  });
})();

基于Stack Overflow回答的代码:How am I supposed to implement multiple select option in angular-material?

答案 1 :(得分:7)

您可以查看指令here的文档。

<强>属性

multiple(可选):boolean - 是否是多个。

<md-select ng-model="someModel" placeholder="Select a state" multiple="true">
    <md-option ng-value="opt" ng-repeat="opt in neighborhoods2">{{ opt }}</md-option>
</md-select>

如果这不起作用,我听说md-multiple可能有用,但文档尚未更新。但我还是无法验证这一点。

答案 2 :(得分:4)

查看chips

您也可以应用自己的自定义模板。

答案 3 :(得分:0)

我使用以下内容允许我使用ng-repeat和一些多重的md-select,而另一些则不是(从Angular 1.4.7开始):

的index.html

<md-input-container ng-repeat="item in items track by $index">
        <label>{{item.title}}</label>
            <div ng-include="item.multiselect == 'true' ? 'm_selectfragment.html' : 's_selectfragment.html'"></div>
   </md-input-container>

的Javascript

$scope.items = 
        [{title: 'funny',
        selected: '', 
        names: [
            {name: 'some name'}, 
            {name: 'other name'},
            {name: 'more?'}, 
            {name: 'say wha???'}             
            ],
         multiselect: "false"   
        }, 
        {title: 'serious', 
        selected: '', 
        names: [
           {name: 'Obama'},
        {name: 'Trump'},
        {name: 'Hillary'},
        {name: 'Putin'}               
            ],
         multiselect: "true"   
        }];

m_selectfragment.html

<md-select name="item.title" ng-model="item.selected" multiple="true">
<md-option ng-repeat="name in item.names" value="{{name.name}}">{{name.name}}
</md-option>

s_selectfragment.html

<md-select name="item.title" ng-model="item.selected">
<md-option ng-repeat="name in item.names" value="{{name.name}}">{{name.name}}
</md-option>

答案 4 :(得分:0)

我在CodePen中编写了AngualrJS响应式芯片。

签出此here