Javascript 优化angular代码以获得更好的可扩展性和良好的体系结构
我有一个下面的html代码。(我不打算使用*ngFor) 正如您在代码中看到的,它看起来像一个坏代码。 是否有任何其他方法可以使此更干净和可扩展 下面是您可以在代码中进行的一些改进 用户Javascript 优化angular代码以获得更好的可扩展性和良好的体系结构,javascript,angular,typescript,optimization,angular6,Javascript,Angular,Typescript,Optimization,Angular6,我有一个下面的html代码。(我不打算使用*ngFor) 正如您在代码中看到的,它看起来像一个坏代码。 是否有任何其他方法可以使此更干净和可扩展 下面是您可以在代码中进行的一些改进 用户this.resetToRue()如果大多数字段为true,则可以创建一个方法this.resetToFalse(),该方法将所有值设置为false 你不能用布尔数组来代替吗? <div *ngIf = 'showTemplate1'>Template 1</div> <
this.resetToRue()
如果大多数字段为true,则可以创建一个方法this.resetToFalse()
,该方法将所有值设置为false
你不能用布尔数组来代替吗?
<div *ngIf = 'showTemplate1'>Template 1</div>
<div *ngIf = 'showTemplate2'>Template 2</div>
<div *ngIf = 'showTemplate3'>Template 3</div>
<div *ngIf = 'showTemplate4'>Template 4</div>
<div *ngIf = 'showTemplate5'>Template 5</div>
<div *ngIf = 'showTemplate6'>Template 6</div>
<div *ngIf = 'showTemplate7'>Template 7</div>
<div *ngIf = 'showTemplate8'>Template 8</div>
displayTemplates(){
if( condition1 ){
this.showTemplate1 = true;
this.showTemplate2 = true;
this.showTemplate3 = true;
this.showTemplate4 = true;
this.showTemplate5 = true;
this.showTemplate6 = true;
this.showTemplate7 = false;
this.showTemplate8 = false;
}
else if( condition2 ){
this.showTemplate1 = false;
this.showTemplate2 = true;
this.showTemplate3 = true;
this.showTemplate4 = true;
this.showTemplate5 = true;
this.showTemplate6 = true;
this.showTemplate7 = false;
this.showTemplate8 = true;
}
else if( condition3 ){
this.showTemplate1 = true;
this.showTemplate2 = true;
this.showTemplate3 = true;
this.showTemplate4 = true;
this.showTemplate5 = true;
this.showTemplate6 = true;
this.showTemplate7 = true;
this.showTemplate8 = false;
}
}
resetToTrue(){
this.showTemplate1 = true;
this.showTemplate2 = true;
this.showTemplate3 = true;
this.showTemplate4 = true;
this.showTemplate5 = true;
this.showTemplate6 = true;
this.showTemplate7 = true;
this.showTemplate8 = true;
}
displayTemplates(){
if(condition1 ){
this.resetToTrue();
this.showTemplate7 = false;
this.showTemplate8 = false;
}
else if( condition2 ){
this.resetToTrue();
this.showTemplate1 = false;
this.showTemplate7 = false;
}
else if( condition3 ){
this.resetToTrue();
this.showTemplate8 = false;
}
}