Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

stacked directive not executed if ng-if is present #8748

Closed
godmar opened this issue Aug 23, 2014 · 5 comments
Closed

stacked directive not executed if ng-if is present #8748

godmar opened this issue Aug 23, 2014 · 5 comments

Comments

@godmar
Copy link
Contributor

godmar commented Aug 23, 2014

When stacking directives on top of a template-expanding directive, the stacked directive may or may not be executed depending on the template. The presence of ng-if, in particular, appears to influence whether stacked directives are applied. Occurs in 1.2.20 and 1.3.0beta16

See pluncker: http://plnkr.co/edit/7gs4uRP5PDHRKj6MLu2s

godmar referenced this issue Aug 24, 2014
BREAKING CHANGE:

The `replace` flag for defining directives that
replace the element that they are on will be removed in the next
major angular version.
This feature has difficult semantics (e.g. how attributes are merged)
and leads to more problems compared to what it solves.
Also, with WebComponents it is normal to have custom elements in the DOM.
@Narretz
Copy link
Contributor

Narretz commented Sep 1, 2014

That looks strange. ngIf really seems to swallow the other directive when replace=true.
But is there a special reason why you use two directive definitions? (One for the replace + template, one for link)

@godmar
Copy link
Contributor Author

godmar commented Sep 1, 2014

Yes. As you may imagine, my test case is a boiled-down example of what we're really facing. The use case is augmentation of an existing AngularJS application that is created by a third party. We rely on it heavily. Imagine that a third-party, let's say the vendor of a web application, creates the template-expanding directive. We don't control the vendor's code. The vendor provides the option to include custom JavaScript (for widgets, modifications, etc. etc.), JavaScript code which we do control.

Our goal is now to enhance/modify the existing UI, for which we use stacked directives that carry the same name as the vendor's original directives. The interaction of those directives with the original (vendor-provided ones) needs to be well-defined and should not depend on what is in the template, even if replace=true is used, IMO.

@lgalfaso
Copy link
Contributor

lgalfaso commented Jan 3, 2015

this is not related to the fact that there are multiple directives that apply to the same element, in fact, if you move the link function to make it one directive you get the same result.
Anywhere that there is a directive with replace=true, then the content is delegated to this other directive. In this case, the directive ng-if is a directive with replace=true, so at link time you cannot modify the content any more. In a strict sense, you can still do it, but that would be a poor practice. What you should be doing is

  • Adding content after the element
  • Use another directive to add the extra content

@lgalfaso lgalfaso closed this as completed Jan 3, 2015
@godmar
Copy link
Contributor Author

godmar commented Jan 4, 2015

So does this mean that directives that use 'replace=true' cannot be stacked?

In our actual use case, the vendor directive uses replace=true. We cannot add other directives in the way you suggest (unless we completely override the vendor directive, which would make stacking less practical).

@lgalfaso
Copy link
Contributor

lgalfaso commented Jan 4, 2015

there are many things you can still do:

  • Attach to some other element inside the template
  • Add another directive that modifies the DOM on compile
  • Override the template

Without some extra information, it is impossible to know what the best solution would be. Now, this is getting out of topic, so I think it is better to continue the conversation in StackOverflow

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants