P Jagajit Prusty P Jagajit Prusty - 6 months ago 307
AngularJS Question

md-chips with md-select in multi select mode

When I am trying to generate md-chips on selecting multiple values from md-select, It is not working. Does md-chips works only with autocomplete analyser and input field?


<md-chips ng-model="launchAPIQueryParams.type">
<md-select name="launchCalType" ng-model="launchAPIQueryParams.type"
multiple="true" placeholder="Launch Type"
md-on-close='applylaunchFilter("type")'>
<md-option ng-repeat="typeOption in launchTypeOptions" ng-value="typeOption[1]">
{{typeOption[0]}}
</md-option>
</md-select>
</md-chips>

Answer

The short answer: No. <md-chips> component will only takes <input> or <md-autocomplete> into its transcluded context.

However, the same thing can be achieved with md-autocompelet. The key is set md-min-length on <md-autocomplete> to 0 so it will auto show the menu just like what a <md-select> menu would be. Here's an example:

// controller.js
angular
  .moduel('mdChipsDemo', [])
  .controller('MdChipsDemoCtrl', function() {
    var vm = this;
    vm.selectedOption = '';
    vm.searchText = '';
    vm.launchAPIQueryParams = {
        types: [],
    };

    vm.launchTypeOptions = [
        {name: 'Op1', value: 1},
        {name: 'Op2', value: 2},
        {name: 'Op3', value: 3},
        {name: 'Op4', value: 4},
    ];
});

// template.html
<div ng-app="mdChipsDemo" ng-controller="MdChipsDemoCtrl as vm">
<md-chips ng-model="vm.launchAPIQueryParams.types">
  <md-autocomplete
            md-selected-item="vm.selectedOption"
            md-search-text="vm.searchText"
            md-items="typeOption in vm.launchTypeOptions"
            md-item-text="typeOption.name"
            md-min-length="0"
            placeholder="Search for a launchTypeOptions">
      <span md-highlight-text="vm.searchText">{{typeOption.name}}</span>
  </md-autocomplete>
  <md-chip-template>
    <span>{{$chip.name}}</span>
  </md-chip-template>
</md-chips>
</div>

If your ultimate goal is to have multiple select ability, <md-autocomplete> also expose <md-item-template> where you can put your <md-select> in. Check the doc for md-autocomplete and you will see.

Or if you really insist on using <select>, there's an 3rd-party component on npm calls md-chips-select which does what you want. https://www.npmjs.com/package/md-chips-select