我有以下代码:我是否总是必须在angular2中声明一个变量才能获得更改?
这是HTML视图:
<button type="button" (click)="filterIt('male')">Filter male gender</button>
<table>
<ng-container *ngFor="let item of array;let i=index">
<tr class="border-bottom" *ngIf="item.condition==condition_var">
<td>{{i+1}}</td>
<td>{{item.condition}}</td>
</tr>
</ng-container>
</table>
这是打字稿文件(.TS):
condition_var:string;
filterIt(value){
this.condition_var=value;
}
注:数组变量已被填充。 (数组对象: [{}]
)
我的问题是:它是在angular2实践始终声明变量和表达式,ngIf,ngFor等与他们合作,或者,我可以用一个更好的办法,而不是我的填充有太多变数的班级看起来不太好。
更具体地说,是否有更好的方法来编写这段代码?
看看管道,而不是循环思考整个数组过滤它事先。 – Akxe
目前还不清楚你在问什么 –
我已经看过管道,我要做一个自定义的...将完成后发布我的答案 – masterach