简体   繁体   English

Angular2:配对选择器在@Directive中做什么

[英]Angular2: What does paired selector do in @Directive

I have some question about @directive of angular 2, 我对角2的@directive有疑问

I'm following this tutorial 我正在关注本教程

1- If selector has to be like this: 1-如果选择器必须像这样:

selector: '[validateEqual][formControlName],[validateEqual][formControl]

and validateEqual is has to be in constructor like this 和validateEqual是必须在这样的构造函数中

 constructor( @Attribute('validateEqual') public validateEqual: string,
    @Attribute('reverse') public reverse: string) {
}

what does it mean also What is @Attribute 这也意味着什么@Attribute

also if there is multi = true in provider and useExisting 同样在提供者和useExisting中是否有multi = true

  • selector: this means that the directive matches elements that have the attribute validateEqual and one of formControlName or formControl 选择器:这意味着指令匹配具有属性validateEqual以及formControlNameformControl之一的formControl

  • @Attribute injects the value of an static attribute. @Attribute注入静态属性的值。 Normally attributes are read into a directive using @Input() . 通常,使用@Input()将属性读入指令。 @Input() also supports bindings where the input is updated when the value bound to a property (or attribute changes). @Input()还支持绑定,当绑定到属性的值(或属性更改)时,输入会更新。 @Input() values are available in ngOnInit() or ngOnChanges() while values injected with @Attribute() are available in the constructor but aren't updated if they change later. @Input()值在ngOnInit()ngOnChanges()中可用,而使用@Attribute()注入的值在构造函数中可用,但是如果以后更改,则不会更新。
    With the following code @Attribute('validateEqual') would set public validateEqual to foo` 使用以下代码, @Attribute('validateEqual') would set public validateEqual @Attribute('validateEqual') would set to foo`。

<div validateEqual="foo" formControlName="bar">

<!-- this doesn't work with `@Attribute('validateEqual')` 
     because the attribute value is not static -->
<div [validateEqual]="foo" formControlName="bar">
  • multi: true means that one provider token provides an array of elements. multi: true表示一个提供者令牌提供了一组元素。 For example all directives for router support routerLink , router-outlet are provided by ROUTER_DIRECTIVES . 例如,路由器支持的所有指令routerLinkrouter-outletROUTER_DIRECTIVES提供。
    If a new provider is registered with the token ROUTER_DIRECTIVES , then it overrides the previously registered directives. 如果使用令牌ROUTER_DIRECTIVES注册了新的提供程序,则它将覆盖以前注册的指令。 If multi: true (on the first registered and tjhe new provider) is set, the new directives are added to the previously registered directives instead of overriding. 如果multi: true (上注册的第一 tjhe新提供商)设置,新指令将被添加到先前注册的指令,而不是压倒一切。
    When ROUTER_DIRECTIVES is injected ( constructor(@Inject(ROUTER_DIRECTIVES) directives) {} ) an array of directive instances is injected. 当注入ROUTER_DIRECTIVESconstructor(@Inject(ROUTER_DIRECTIVES) directives) {} )时,将注入一系列指令实例。 It usually doesn't make sense to inject ROUTER_DIRECTIVES . 注入ROUTER_DIRECTIVES通常没有任何意义。 I used it just as an example because it is multi: true . 我将其用作示例,因为它是multi: true

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

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