(onSelectChange) output for mdOption ALWAYS gets passed first item in list

久未见 提交于 2019-12-01 10:49:54

With onSelectionChange event, it is possible to differentiate when the item is selected or unselected. You need to pass an $event to the target method in order to differentiate between the two cases.

Here are the changes you need to make in order to make your changes work:

In your component html:

<md-autocomplete [displayWith]="displayRole" #auto="mdAutocomplete">
    <md-option 
        *ngFor="let role of roles | acrole: roleField.value | slice:0:4; let i=index;" 
        [value]="role" 
        (onSelectionChange)="AddRole($event, role)">

        <div class="label">
            {{role.label}}
        </div>
    </md-option>
</md-autocomplete>

... and in your component.ts, change the method to following:

AddRole(event: MdOptionSelectionChange, role: Role) {  
    if (event.source.selected) {
        this.selectedList.push(role);
    }
}

Here is a PLUNKER DEMO based on the Material documentation example. The documentation needs to be a bit more clear about these changes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!