How to bind to multiple in md-select inside directive

前端 未结 1 1954
再見小時候
再見小時候 2021-01-24 10:20

Trying to create a simple directive that shows a textbox or dropdown depending on whether an array is passed for a model property on the scope.

Anything exc

相关标签:
1条回答
  • 2021-01-24 10:52

    Probably this is not the answer you are looking for...

    I've tried to conditionally set multiple attribute to md-select and nothing seems to be work (ng-attr-multiple, ng-multiple...). Probably it's an angular-material bug.

    So, as a workaround, you could conditionally add two md-selects, depending on the atribute model.multiSelect value: one with multiple attribute and the other one without it. Example:

    <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>\
    

    IMPORTANT: Keep in mind that if the md-select is multiple, the value binded needs to be an array, so you will have to change ng-model="value" per ng-model="[value]", as you can see in the previous code.

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

    Hope it helps. Anyway, I'll be waiting for other answers.

    0 讨论(0)
提交回复
热议问题