下面是我的代码,我省略了组件代码的缩写:
模板.html
...
<tr *ngFor="let item of getProducts(); let i = index"
[pa-attr]="getProducts().length < 6 ? 'bg-success' : 'bg-warning'">
<td>{{item.name}}</td>
</tr>
以下是自定义属性指令代码:
@Directive({
selector: "[pa-attr]",
})
export class PaAttrDirective {
constructor(private element: ElementRef) {
console.log('been called')
}
@Input("pa-attr")
bgClass: string;
ngOnInit() {
this.element.nativeElement.classList.add(this.bgClass || "bg-success",
"text-white");
}
}
目前我有 5 个项目:
然后我添加一个新项目,然后我有:
我在这里很困惑,为什么只有第六项的颜色更改为黄色(bg-warning)?是否应该将所有项目的颜色都更改为黄色?因为当我添加一个新项目时,数据源发生了getProducts()
变化,所以getProducts().length
返回了6个项目,既然数据源发生了变化,那么整体<tr>
应该重新评估,所以现在每个项目的第1到第6个应该是黄色的,不是吗?那么为什么只有第六项是黄色的?
元芳怎么了
慕盖茨4494581
相关分类