Plunkr: http://plnkr.co/edit/y4e4jS89gOxRKQOyUW2r?p=preview
I'm using the selectionChange @Output on a mat-chip to see the resulting behavior of chip selection but it seems that the eventEmitter isn't firing on chip selection?
.html:
<mat-chip-list>
<mat-chip (selectionChange)="changeSelected($event)">Papadum</mat-chip>
<mat-chip (selectionChange)="changeSelected($event)">Naan</mat-chip>
<mat-chip (selectionChange)="changeSelected($event)">Dal</mat-chip>
</mat-chip-list>
<p>Currently Selected: {{selected}}</p>
.ts:
selected: string;
changeSelected(e) {
console.log(e);
this.selected = e.value;
}
In this case, no event is emitted at all on click selection. Is this something that is still in development, or does selection mean something different from what I am thinking of?
Not sure what the purpose of this component is, since it's still a work in progress, but it seems to be about providing an interface to disabling and enable selectable content, and some other features.
You're not seeing any events firing because you haven't activated selected.
In your case, something like this will resolve it.
<mat-chip-list>
<mat-chip [selected]="state1" (selectionChange)="changeSelected($event)" (click)="state1=!state1">Papadum</mat-chip>
<mat-chip [selected]="state2" (selectionChange)="changeSelected($event)" (click)="state2=!state2"> Naan</mat-chip>
<mat-chip [selected]="state3" (selectionChange)="changeSelected($event)" (click)="state3=!state3"> Dal</mat-chip>
</mat-chip-list>
Also if you want to make this more generic, resort to *ngFor
directive
in html
<mat-chip-list>
<mat-chip *ngFor="let chip of chips" [selected]="chip.state" (selectionChange)="changeSelected($event)" (click)="chip.state=!chip.state">{{chip.name}}</mat-chip>
</mat-chip-list>
in ts
chips = [
{name: 'Papadum'},
{name: 'Naan'},
{name: 'Dal'}
];
Here is a sample which I was able to use to make it working. works even if the categories are selected by default. You can also use select()
method instead of selectViaInteraction()
which I have used in the demo.
HTML:
<mat-chip-list #chipList [multiple]="true" [selectable]="true">
<mat-chip *ngFor="let category of categories" #chip="matChip" (click)="category.selected ? chip.deselect() : chip.selectViaInteraction()" [selected]="category.selected" [selectable]="true" class="leadr-category-chip" (selectionChange)="changeSelected($event, category)">
{{category.name}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
</mat-chip-list>
TS:
changeSelected($event, category): void {
category.selected = $event.selected;
}
SAMPLE:
this.categories = [
{
name: 'Category 1',
selected: false
},
{
name: 'Category 2',
selected: true
},
{
name: 'Category 3',
selected: false
},
{
name: 'Category 4',
selected: true
},
{
name: 'Category 5',
selected: false
}
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With