-
Notifications
You must be signed in to change notification settings - Fork 27.3k
Fix merging of two ngIfs in replace mode. #10733
Conversation
I do not know if I want to go this route... if we start adding special rules for every directive, then sooner or later we will end up with too many rules on how attributes should be merged |
Do you have any other idea for fixing this issue? |
The way I see it ngIf is a special directive when it comes to merging its conditions and as such it should be treated differently in the code. How else can we get this working correctly? |
Wouldn't the same be true for ngHide and ngShow? |
@realityking You're absolutely right, ngShow, ngHide and also ngDisabled, ngOpen. Probably some others too, but this is what I gather from a quick look at the list of built-in directives. Basically it would affect any directive with a boolean condition. Anybody recalls other such directives apart from the ones already mentioned above? I would be happy to add all identified directives to the fix, but first I would like the community to help me find the best solution to this problem. |
Agree with @lgalfaso. Also it should be noted that we recommend against using the replace option. |
Angular has its limitations whenever there are multiple directives in the There are other directives that also have issues getting merged, an These are some of the reasons why replace: true is deprecated (and its use I am sorry that you are running into this issue, but there is currently no |
@lgalfaso Thanks for this in-depth explanation. I did not know that replace was deprecated. This decision makes a lot of sense now that you have laid it out so clearly. My example (http://codepen.io/kubawalinski/pen/EaWjxe?editors=101) was deliberately simplified, but in real life the situation that I encountered was that I was using somebody else's directive from npm and I wanted to use ngIf on it, but it failed due to the fact that there was an inner ngIf on a root element inside that directive. I guess the way forward in such situations is to use ngShow if a directive uses ngIf and vice versa. It's not ideal, as I know some developers like to control their resulting markup pretty tightly. However, I see no other way around such an issue at this time. Would you guys agree? |
The use of ngIf and ngShow combination is a solution. BTW (and I know you |
BTW, (if your usecase allows it), you could wrap the directive in a parent element and put |
Please do not do this. If this in fact works, then it is an oversight and may break in the future. I am closing this issue now |
Hm...it does indeed work, but why is it an oversight ? Why do you say it shouldn't work ? Just to make sure we are on the same page, I am talking about something like this: HTML<div ng-if="expr1">
<test show="expr2"></test>
<div> JS.directive('test', function testDirective() {
return {
template: '<span ng-if="show">Hello, world !</span>',
scope: {show: '='},
replace: true
};
} So, @lgalfaso, are you saying the above shouldn't be done (and might break in the future) ? Why is that ? |
@gkalpak I thought you meant that if in the outer element you have an attribute named |
I always refer to directives by their normalized named 😃 |
I noticed an error while working with ngIf in my project. It is reproduced here: http://codepen.io/kubawalinski/pen/EaWjxe?editors=101. In short, when 2 ngIfs need to be merged the merge is done incorrectly (it just concatenates the conditions with a space character). This is an attempt to fix this by concatenating with the ' && ' string, so that both conditions are checked and both need to be true for the ngIf to kick in.