解决的办法是创建一个属性指令,通过这个指令我们可以设置元素的显示属性。
showcolumn.directive.ts
:
import {Component, Directive, ElementRef, Input, AfterViewInit} from '@angular/core';
@Directive({
selector: '[showColumn]'
})
export class ShowColumnDirective implements AfterViewInit{
@Input() showInput: string;
constructor(private elRef: ElementRef) {
}
ngAfterViewInit(): void {
this.elRef.nativeElement.style.display = this.showInput;
}
}
声明这个指令你的应用程序的模块或任何其他模块:
import {ShowColumnDirective} from './showcolumn.directive';
@NgModule({
declarations: [ShowColumnDirective]
})
export class AppModule {}
现在我们可以使用该指令我们的表的HTML组件中:
<ng-container matColumnDef="position">
<mat-header-cell showColumn showInput="none" *matHeaderCellDef> No. </mat-header-cell>
<mat-cell showColumn showInput="none" *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
下面是一个演示:https://plnkr.co/edit/Woyrb9Yx8b9KU3hv4RsZ?p=preview
@ kelum-priyadarshane你试过解决方案吗? –