-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Can't update DOM-node attr while using {{}} expression #12813
Comments
You shouldn't be doing any DOM manipulation in a controller only time a DOM access/mutation should occur is in a directive. If you use the ng-style directive you can avoid the destructive behavior even leaving the DOM manipulation in the controller but I would strong strongly advise against it http://jsfiddle.net/8tceyw2w/10/ |
@shusain, the point is that trying to externally change an attribute's interpolated value is broken. (Where that happens from (e.g. directive, controller etc) is not really important here.) So, what you say is true in general, but this is just a POC of an actual bug. |
I see what you're saying but following certain rules even when just reproducing an issue seems reasonable to do anyhow. (seems you both know what you're talking about but less knowledgeable devs may see this and think it's the right way to do things) I can see how this would happen since when the interpolation happens to process the style attribute it's not aware of the styles set on the element directly and so it ignores those but the padding-left is still part of the attribute value that it'll apply after parsing/processing the interpolated value. I suppose the behavior you're expecting is that the style interpolation doesn't remove any existing styles that the interpolated value itself doesn't replace but this seems like it will be a confusing solution itself. |
I'd also say that this can be expected, given how angular works. |
(Moved this comment from the closed #9109 issue). This issue is not fixed untill latest 1.4.7 version. I'm using Angular 1.4.7 (also tested in 1.4.5 and 1.4.6) — I have the same issues as described in this topic — if I have 2 or more (In fact, they are not working only in first digest cycle — after the scope is updated second time (after some action which updates the scope, for example), everything works as expected :) So, here All functions return
|
@artuska Yours is actually a slightly different problem (possibly caused by the same underlying issue). Question: why do you want to use interpolation and ngClass at the same time? |
@Narretz because of code style:
Also, because I cannot set all those |
I ran into this in Angular v1.5.5 as well. I just fixed it like this...
to
or if your class name contains a variable
|
@zweissman thanks, that helped. In my case I had to wrap the interpolated classes in single quotes
|
… of interpolated strings) Closes angular#12813
… of interpolated strings) Closes angular#12813
There is a kind of race condition between dom-node attr internal recalculation when we are using {{}}-expression and dom-node attr external modifying. This topic is based on #12763 issue and also related to #9109 issue. Also I'd like to thank @gkalpak for he clarified a problem.
Here is a little repro on this issue: http://jsfiddle.net/dhilt/8tceyw2w/9/
I have an element with style-attr contains {{}}-expression:
and I want to play with some style property which isn't presented in the markup:
Let's run the demo!
1. Initial state. Before 1st digest ends.
Run the demo. Get the alert before out from $watch.
We see that both of padding-left and padding-top properties are set to 15 and 20 px.
And all of them are applied properly; we can see it despite the UI data is still unbound cause of alert-interruption.
2. Initial state. The end of initial digest.
Close the alert. Let the 1st digest cycle end.
See that padding-top is no longer applied to the element.
Something is broken and initial template's value of style-attr has a destructive priority over further updates on it.
3. Recalculate style expression via $scope.toggle changing.
Click on checkbox, start the new digest cycle.
We see that there is no problem with inline padding-left property.
But padding-top property is completely removed from element's style. Just an empty string.
And
myElementStyle.paddingTop = '20px'
within $watch doesn't work..
Thanks and good luck)
The text was updated successfully, but these errors were encountered: