Jagajit Prusty
Jagajit Prusty

Reputation: 2110

Angular Material: make certain chips disable conditionally in md-chips?

I am using md-chips directive to generate chips.However I want certain chips to be conditionally disabled , while keeping others editable. But according to documentation, either we can make all chips disabled or none.

<md-chips ng-model="FruitNames" name="fruitName" readonly="$chip.notEditable == 'true'" md-max-chips="5">
    <md-chip-template>
      <strong>{{$chip}}</strong>
      <em>(fruit)</em>
    </md-chip-template>
  </md-chips>

Is there any way to make chips conditionally editable/non-editable?

Upvotes: 4

Views: 3233

Answers (1)

Mikhail Asavkin
Mikhail Asavkin

Reputation: 31

You can use md-chip-remove directive to add your own remove chip button and put disabling condition there on the chip level. See the example below:

<md-chips ng-model="FruitNames" name="fruitName" md-max-chips="5" md-on-remove="onRemovedChip($chip)">
    <button
        ng-if="!$chip.notEditable"
        class="md-chip-remove ng-scope" 
        md-chip-remove
        type="button"
        aria-hidden="true" tabindex="-1">
        <md-icon md-svg-icon="md-close"></md-icon>
        <span class="md-visually-hidden ng-binding">Remove</span>
    </button>
    <md-chip-template>
        <strong>{{$chip}}</strong>
        <em>(fruit)</em>
    </md-chip-template>
</md-chips>

In order to forbid tags removal using backspace, you should also add this method to your controller:

$scope.onRemovedChip = function(chip) {
    if (chip.notEditable) {
        $scope.FruitNames.push(chip);
    }
};

Upvotes: 3

Related Questions