简体   繁体   English

混淆[[ngModel)]不在Angular4的任何指令类别中

[英]Confused as [(ngModel)] doesn't fall in any of the Directive Categories in Angular4

As per Angular Docs the Directives of Angular are of 3 types: 根据Angular Docs,Angular的指令有3种类型:

  • Components 组件
  • Structural 结构
  • Attribute 属性

As per the following official link: https://angular.io/guide/attribute-directives#directives-overview 按照以下官方链接: https : //angular.io/guide/attribute-directives#directives-overview

1. Components 1.组成

<home></home>
<about-us></about-us>
<support></support>

2. Structural Directives 2.结构指令

<div *ngIf="age < 18"> ... </div>
<div *ngFor="let x of students"> ... </div>

3. Attribute Directives 3.属性指令

<div [ngClass]="red"> ... </div>
<div [ngStyle]="{'background':colorValue}> ... </div> 

Now, my problem is that [(ngModel)] is a directive, but I am confused as it doesn't seem to fall in any of the above 3 categories (as per official documentation), and there is no 4th category! 现在,我的问题是[[ngModel)]是一个指令,但是我很困惑,因为它似乎不属于上述3类(根据官方文档),并且没有第4类! So, can someone point out what kind of directive is [(ngModel)]? 因此,有人可以指出[(ngModel)]是哪种指令?

[(ngModel)] is a 'convenience' directive used by Angular to simplify two-way binding by combining a attribute directive and an event-listener. [(ngModel)]是Angular使用的“便捷”指令,它通过组合属性指令和事件侦听器来简化双向绑定。 It doesn't really belong into any of the directive groups you mentioned, but it's also not a group of its own. 它实际上并不属于您提到的任何指令组,但它也不是其自己的组。

Using the @Directive Decorator , you could also create your own directives, the types offered by the Angular Documentation are probably meant to ease the entry into Angular (.. or in your case, offer potential for confusion ;-) ) 使用@Directive Decorator ,您还可以创建自己的指令,Angular Documentation提供的类型可能是为了简化Angular的输入(..或在您的情况下,可能会造成混淆;-))

[(NgModel)] is a attribute directive combined with the event listener. [(NgModel)]是与事件侦听器组合的属性指令。 This [(NgModel)] is a shorthand of: [(NgModel)]是以下内容的简写:

[ngModel]="variable" (ngModelChange)="variable = $event" . [ngModel]="variable" (ngModelChange)="variable = $event" As you see, behind the scenes it hides something similar to the EventEmitter. 如您所见,它在幕后隐藏了类似于EventEmitter的东西。 So it binds variable to the template and also listens for a change event in both - template and model. 因此,它将variable绑定到模板,还监听模板和模型中的更改事件。

NgModel is an Attribute Directive . NgModel是一个属性指令 It is applied as an attribute to (almost) any element in your DOM. 它作为属性应用于(几乎)DOM中的任何元素。

The doc for NgModel shows that its selector is [ngModel]... , which means it is applied as an attribute to (almost) any element in your DOM. NgModel文档显示其选择器为[ngModel]... ,这意味着它被作为属性应用于(几乎)DOM中的任何元素。

The official article that you linked summarizes the three categories: 您链接的官方文章总结了三个类别:

Components — directives with a template. 组件-带有模板的指令。

Structural directives — change the DOM layout by adding and removing DOM elements. 结构化指令-通过添加和删除DOM元素来更改DOM布局。

Attribute directives — change the appearance or behavior of an element, component, or another directive. 属性指令-更改元素,组件或其他指令的外观或行为。

When you put [(ngModel)] on an element, you are modifying its behavior by associating inputs and outputs with it (see @JensHabegger 's answer). 当将[(ngModel)]放在元素上时,您正在通过将输入和输出与元素相关联来修改其行为(请参见@JensHabegger的回答)。 The "banana in a box" is syntactic sugar for two-way binding which is detailed here . “盒子里的香蕉”是用于双向绑定的语法糖, 在此处进行了详细说明。 Essentially, you are applying [ngModel] with an input, then automatically modifying a value based on its output. 本质上,您将[ngModel]与输入一起应用,然后根据其输出自动修改值。

As such, NgModel is definitely an attribute directive. 因此, NgModel绝对是属性指令。

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

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