gpt4 book ai didi

javascript - ngSwitch 是 "Attribute Directive"还是 "Structural Directive"?

转载 作者:行者123 更新时间:2023-11-30 11:12:26 28 4
gpt4 key购买 nike

我对 ngSwitch 指令有点困惑——它是“属性指令”还是“结构指令”

属性指令用“方括号”编写,如 [ngStyle]、[ngClass] 等(我们将其写为 [ngSwitch],将其称为“属性指令”)。

结构指令使用“aestrick”编写,如 *ngFor、*ngIf 等(我们将 case 写为 *ngSwitchCase="...",这意味着它是结构指令) .

<div [ngSwitch]="colorValue">
<p *ngSwitchCase="red">Red</p>
<p *ngSwitchCase="blue">Blue</p>
<p *ngSwitchCase="green">Green</p>
</div>

根据上面的代码,将 ngSwtich 归类到任何一个指令类别都变得非常困惑!有人可以帮助我理解 ngSwitch 的指令类型吗?

最佳答案

[ngSwitch] 是一个属性指令,与 *ngSwitchCase*ngSwitchDefault 结合使用结构指令

这在 Angular 的文档中有清楚的解释...

  • NgSwitch — an attribute directive that changes the behavior of its companion directives.
  • NgSwitchCasestructural directive that adds its element to the DOM when its bound value equals the switch value and removes its bound value when it doesn't equal the switch value.
  • NgSwitchDefaultstructural directive that adds its element to the DOM when there is no selected NgSwitchCase.

https://angular.io/guide/built-in-directives#switching-cases-with-ngswitch

关于javascript - ngSwitch 是 "Attribute Directive"还是 "Structural Directive"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53147585/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com