2

I have a directive A that I can't change (a predefined one). I want to create another directive B where the link function should be called first (before A). How can I do it? (let's say I don't know what is the priority of directive A - if it's relevant at all...).

The structure of the html is that directive B is a child of directive A:

<DirectiveA><DirectiveB></DirectiveB></DirectiveA>
1

1 Answer 1

1

Answer:

  • The priority is not relevant, it only matters when you have more than one directive on the same element.
  • You don't need to do anything special as it is already the case. Child elements post linking functions always run before their parent's post linking function.
  • If DirectiveA ( the parent element ) is deferring the creation of DirectiveB then you cannot do anything from inside DirectiveB to prevent it.
  • ngRepeat / ngIf / ngSwitch / ngView / ngInclude are all good examples of directives which defer the creation of their child elements.

Sources

From $compile docs

priority

When there are multiple directives defined on a single DOM element, sometimes it is necessary to specify the order in which the directives are applied.

From Understanding-Directives article:

Pre vs Post Linking Functions

The difference is that PreLinkingFunction() will fire on the parent first, then child, and so on. A PostLinkingFunction() goes in reverse, firing on the child first, then parent, and so on.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.