简体   繁体   English

ngSwitch 是“属性指令”还是“结构指令”?

[英]ngSwitch is "Attribute Directive" OR "Structural Directive" ?

I am a bit confused regarding ngSwitch directive -- whether it is 'attribute directive' or 'structural directive' .我对 ngSwitch 指令有点困惑——无论是“属性指令”还是“结构指令”

Attribute directives are written with ' square brackets ' like [ngStyle], [ngClass], etc. (and we write it as [ngSwitch] which refers it as 'Attribute Directives').属性指令用“方括号”编写,如 [ngStyle]、[ngClass] 等(我们将其写为 [ngSwitch],将其称为“属性指令”)。

Structural directives are written with ' aestrick ' like *ngFor, *ngIf, etc. (and we write the cases as *ngSwitchCase="..." which means it is a structural directive).结构指令用' aestrick ' 编写,如*ngFor、*ngIf 等(我们将case 写为*ngSwitchCase="...",这意味着它是一个结构指令)。

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

As per the code above, it is getting very confusing to categorize ngSwtich to either of the Directive Categories!根据上面的代码,将 ngSwtich 归类到任一指令类别会变得非常混乱! Can someone help me out in understanding the directive-type of ngSwitch ?有人可以帮助我理解 ngSwitch 的指令类型吗?

[ngSwitch] is an attribute directive used in combination with *ngSwitchCase and *ngSwitchDefault that are both structural directives . [ngSwitch]是一个属性指令,与都是结构指令的*ngSwitchCase*ngSwitchDefault结合使用。

This is clearly explained in Angular's documentation...这在 Angular 的文档中有清楚的解释......

  • NgSwitch — an attribute directive that changes the behavior of its companion directives. NgSwitch — 一个属性指令,改变其伴随指令的行为。
  • 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. NgSwitchCase结构指令,当它的绑定值等于 switch 值时将其元素添加到 DOM,并在它不等于 switch 值时删除其绑定值。
  • NgSwitchDefaultstructural directive that adds its element to the DOM when there is no selected NgSwitchCase. NgSwitchDefault结构指令,当没有选择的 NgSwitchCase 时将其元素添加到 DOM。

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

It is a structural directive这是一个结构性指令

Structural directives updates the DOM layout by adding or removing elements.结构指令通过添加或删除元素来更新 DOM 布局。

As I understand it , 'structural directive' change the dom's struct.据我了解, “结构指令”会更改 dom 的结构。 attribute directive change the dom's attribute,such as color,background and so on属性指令改变dom的属性,如颜色、背景等

ngSwitch change it's children's length , so its a structural directive , ngSwitch改变它的孩子的长度,所以它是一个结构指令

Structural Directive:结构指令:

What are structural directives?什么是结构指令?

Structural directives are responsible for HTML layout.结构指令负责 HTML 布局。 They shape or reshape the DOM's structure, typically by adding, removing, or manipulating elements.它们通常通过添加、删除或操作元素来塑造或重塑 DOM 的结构。

As with other directives, you apply a structural directive to a host element.与其他指令一样,您将结构指令应用于宿主元素。 The directive then does whatever it's supposed to do with that host element and its descendants.该指令然后对该宿主元素及其后代执行它应该做的任何事情。

Structural directives are easy to recognize.结构指令很容易识别。 An asterisk (*) precedes the directive attribute name as in this example.星号 (*) 位于指令属性名称之前,如本例所示。

Refer: https://angular.io/guide/structural-directives参考: https : //angular.io/guide/structural-directives

ngSwitch is a built-in structural directive. ngSwitch 是一个内置的结构指令。 [ https://angular.io/guide/structural-directives] [ https://angular.io/guide/structural-directives ]

@j3ff: The (*) star on *ngSwitchCase is merely sugar syntax, it does not indicate the type of directive. @j3ff:*ngSwitchCase 上的 (*) 星号只是语法糖,并不表示指令的类型。

It could be written the long way like this...它可以像这样写得很长......

<div class="course-category" [ngSwitch]="colorValue">
 <ng-template [ngSwitchCase]="'RED'">
  <div>
   <p>Red</p>
  </div>
 </ng-template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM