Controlling order of directive evaluation in Angular 2

╄→尐↘猪︶ㄣ 提交于 2019-12-04 03:24:34

问题


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


回答1:


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




回答2:


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]
})


来源:https://stackoverflow.com/questions/35756253/controlling-order-of-directive-evaluation-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!