Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling order of directive evaluation in Angular 2

I want to create an attribute directive in Angular 2. It needs to have a click handler on its host. The click handler needs to be added before the other directives on the element are evaluated because it controls access to certain functionality. In Angular 1, you could do this with the priority option when creating a directive. Is there some sort of equivalent in Angular 2?

Thanks, Chase

like image 464
Chase M Gray Avatar asked Mar 02 '16 19:03

Chase M Gray


2 Answers

priority in Angular 2 is not supported, and there isn't any plan to add it.

Component directives may not use the following attributes:

priority and terminal. While Angular 1 components may use these, they are not used in Angular 2 and it is better not to write code that relies on them.

See https://angular.io/docs/ts/latest/guide/upgrade.html#!#using-component-directives

like image 112
Mark Kennedy Avatar answered Oct 15 '22 22:10

Mark Kennedy


I found that the order the directives are evaluated in in Angular 2 can be defined in the declarations block of the ngModule decorator. Like this:

@NgModule({
    imports: [BrowserModule], 
    // SecondDirective will be evaluated before FirstDirective
    declarations: [AppComponent, SecondDirective, FirstDirective],
    bootstrap: [AppComponent]
})
like image 37
notme1994 Avatar answered Oct 16 '22 00:10

notme1994