Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Directive execution order in angular 2

If I have a simple button with a click handler and a custom attribute directive like so:

<button  (click)="save()" attributedirective="project saved">Save</button>

And in my attribute directive I'm using the hostlistener decorator to listen to the click event:

@Directive({
    selector: `[attributedirective]`
})
export class AuditPusher {
    @Input('attributedirective') attributedirective: string = 'Missing message!';

    @HostListener('click', ['$event'])
    pushAudit() {
        console.log('text:'+this.attributedirective.toString());
    }
}

Which of my code will fire first? The save() on the click event or the code in my attribute directive? - And: Imagine having two attribute directives. Which of those will fire first? In Angular 1 there was something like directive priorities, how is this done in Angular 2? I find it difficult to find documentation on this.

like image 211
jo v Avatar asked Aug 09 '16 10:08

jo v


People also ask

What are directives in angular 2?

A directive is a custom HTML element that is used to extend the power of HTML. Angular 2 has the following directives that get called as part of the BrowserModule module. If you view the app.

What is the difference between @component and @directive in Angular?

Components have their own view (HTML and styles). Directives are just "behavior" added to existing elements and components. Component extends Directive . Because of that there can only be one component on a host element, but multiple directives.

What is @directive in Angular?

What is meant by directives in Angular? Directives are classes that add new behavior or modify the existing behavior to the elements in the template. Basically directives are used to manipulate the DOM, for example adding/removing the element from DOM or changing the appearance of the DOM elements.


1 Answers

As far as I know the order of execution is undefined. You shouldn't depend on a specific order.

like image 55
Günter Zöchbauer Avatar answered Oct 06 '22 18:10

Günter Zöchbauer