如何在 md-select 内部指令中绑定到多个

How to bind to multiple in md-select inside directive

尝试创建一个简单的指令来显示文本框或下拉列表,具体取决于是否为范围上的 model 属性 传递数组。

除了在指令标记中显式设置 false 之外的任何内容,例如 multiple="false",都会导致多 select 下拉列表。

为什么这不起作用?此外,md-select 值绑定不起作用(尽管文本框绑定起作用),我怀疑出于同样的原因。

Plunkr available here illustrating the issue

消费者

<div ng-app='home' layout-padding layout="row">
  <content-filter ng-repeat="filter in filters" flex="filter.width" model="filter.model" value="filter.value"></content-filter>
</div>

消费者控制器

app.controller('MainCtrl', function($scope) 
{
  $scope.filters = 
  [
    {
      model: 
      {
       multiSelect: false,
        items: 
        [
          {
            label: 'All',
            value: 'all'
          }, 
          {
            label: 'Fail',
            value: 'fail'
          }, 
          {
            label: 'Success',
            value: 'success'
          }
        ]
      },
      value: 'all',
      width: '50%'
    }, 
    {
      value: '123',
      width: '50%'
    }
  ];
 });

指令

app.directive('contentFilter', function() 
{
  return {
    restrict: 'E',
    replace: false,
    template: '\
      <md-input-container flex layout="fill" ng-if="model && model.items.length">\
        <md-select ng-model="value" multiple="model.multiSelect === true">\
         <md-option ng-repeat="item in model.items" ng-value="{{ item.value }}">{{ item.label }}</md-option>\
        </md-select>\
      </md-input-container>\
      <md-input-container flex layout="fill" ng-if="!model">\
        <input type="text" aria-label="{{ label }}" ng-model="value" />\
      </md-input-container>',
    scope: 
    {
      value: '=',      
      model: '=?'
    }
  };
});

可能这不是您要找的答案...

我尝试有条件地将多个属性设置为 md-select,但似乎没有任何效果(ng-attr-multipleng-multiple...)。可能是 angular-material 错误。

因此,作为一种解决方法,您可以有条件地添加两个 md-select,具体取决于属性 model.multiSelect 值:一个具有多个属性,另一个没有。示例:

<md-input-container flex layout="fill" ng-if="model && !model.multiSelect && model.items.length">\
    <md-select ng-model="value">\
     <md-option ng-repeat="item in model.items" value="{{ item.value }}">{{ item.label }}</md-option>\
    </md-select>\
</md-input-container>\
<md-input-container flex layout="fill" ng-if="model && model.multiSelect && model.items.length">\
    <md-select ng-model="[value]" multiple>\
     <md-option ng-repeat="item in model.items" value="{{ item.value }}">{{ item.label }}</md-option>\
    </md-select>\
</md-input-container>\

重要提示:请记住,如果 md-select 是多个,则绑定的值需要是一个数组,因此您必须根据 ng-model="[value]" 更改 ng-model="value",因为可以在前面的代码中看到。

I've forked your plunker and you can see a working example here

希望对您有所帮助。无论如何,我会等待其他答案。