这是我的 emp 对象数组:
emp[
{"id":1001,"name":"robin_delhi"},
{"id":1002,"name":"robin_mumbai"},
{"id":1003,"name":"robin_bang"},
{"id":1004,"name":"sushil_delhi"},
{"id":1005,"name":"sushil_mumbai"},
]
这是我的 HTML 模板,我通过角材料的垫选择组件在下拉列表中显示 emp 对象:
<mat-form-field>
<mat-select [(ngModel)]="emp" [(value)]="sel" placeholder="Employee
[formControl]="toppings" multiple required>
<mat-option *ngFor="let p of emp | proDisable:emp:sel; let p = index;" [value]="p">
{{p.name}}
</mat-option>
</mat-select>
</mat-form-field>
I want when particular emp name will selected, it will match with remaining element (first part of special character “_”), if match found it will show as usual else disabled.
这是我的自定义管道:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'proDisable'
})
export class ProDisablePipe implements PipeTransform {
transform(value:any, data:any, sel:any) : any {
if(!sel)
{
return value;
}
else
{
let nval:any;
for(var i=0; i<data.length; i++)
{
for(var j=0; j<sel.length; j++)
{
if ((i['name'].substring(0, i['name'].indexOf("_")))==(sel['name'].substring(0, sel['name'].indexOf("_"))))
{
nval= "class='disable'"+value;
}
else
{
nval= "class='enable'"+value;
}
}
}
return nval;
}
}
}
在这里,如果我选择第一个元素,最后两个将被禁用。
请帮助如何实现这一目标或任何更好的方法来做到这一点。