所以我学习了 Angular 并开始学习 switch 语句,但是我对有关 quotation makrs 的 switch 语句的 div 标签分配感到困惑。这是html代码:
<div [ngSwitch]="color">
<div *ngSwitchCase="red">You picked red color</div>
<div *ngSwitchCase="blue">You picked blue color</div>
<div *ngSwitchCase="green">You picked green color</div>
</div>
我已经声明了一个变量 color
在我的.ts
文件
export class StructuralDividesComponent implements OnInit {
public color="red";
constructor() { }
ngOnInit() {
}
}
为什么此代码仅在 color
时才有效值在 "'red'"
而不是 "red"
.
最佳答案
我认为这是核心 JavaScript 概念。当您给出 *ngSwitchCase="red"时,它会尝试从其组件中查找红色变量并替换该值。但是如果你想将它与字符串值匹配,那么你必须编写 *ngSwitchCase="'red'"
关于html - 使用 ""和 ' ' 分配标签之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54663633/