简体   繁体   中英

Angular2: What does paired selector do in @Directive

I have some question about @directive of angular 2,

I'm following this tutorial

1- If selector has to be like this:

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

and validateEqual is has to be in constructor like this

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

what does it mean also What is @Attribute

also if there is multi = true in provider and useExisting

  • selector: this means that the directive matches elements that have the attribute validateEqual and one of formControlName or formControl

  • @Attribute injects the value of an static attribute. Normally attributes are read into a directive using @Input() . @Input() also supports bindings where the input is updated when the value bound to a property (or attribute changes). @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.
    With the following code @Attribute('validateEqual') would set public validateEqual 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. For example all directives for router support routerLink , router-outlet are provided by ROUTER_DIRECTIVES .
    If a new provider is registered with the token ROUTER_DIRECTIVES , then it overrides the previously registered 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.
    When ROUTER_DIRECTIVES is injected ( constructor(@Inject(ROUTER_DIRECTIVES) directives) {} ) an array of directive instances is injected. It usually doesn't make sense to inject ROUTER_DIRECTIVES . I used it just as an example because it is multi: true .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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