admin管理员组文章数量:1287290
In the below template, I would expect the script tag to never render, and the alert script to never execute. However it does.
<div ng-if="false">
<script>alert('should not run')</script>
Should not appear
</div>
This is causing us huge performance problems on mobile devices as we wrap large DOM and directive structures in ng-if
s with the expectation they will not render when the condition is false.
I have also tested ng-switch
which behaves in the same manner.
Is this expected behaviour? Is there a way to avoid the unnecessary render?
JSFiddle
In the below template, I would expect the script tag to never render, and the alert script to never execute. However it does.
<div ng-if="false">
<script>alert('should not run')</script>
Should not appear
</div>
This is causing us huge performance problems on mobile devices as we wrap large DOM and directive structures in ng-if
s with the expectation they will not render when the condition is false.
I have also tested ng-switch
which behaves in the same manner.
Is this expected behaviour? Is there a way to avoid the unnecessary render?
JSFiddle
Share Improve this question asked Mar 18, 2015 at 11:18 WearyMonkeyWearyMonkey 2,6191 gold badge26 silver badges22 bronze badges 2- 2 Have you read this? github./angular/angular.js/issues/3954 – paul Commented Mar 18, 2015 at 11:31
- try <div ng-if="'false'"> – changtung Commented Mar 18, 2015 at 12:08
2 Answers
Reset to default 8It may seem backward, but ngIf
deals more with the removal of DOM, rather than the addition. Before the controller finishes instantiating, the DOM still exists. This is generally a good thing, and allows you to have graceful degradation for users without JS (or, alternatively, an initial loading state).
If you don't want the inner DOM to render, place it in a directive (either its own directive, or via ng-include
) or in a view.
Example 1 (understanding why the script runs):
To help yourself understand the flow a bit better, you can update the example to instead be:
<div ng-if="false">
{{"Should not appear"}}
<script>alert('should not run')</script>
</div>
https://jsfiddle/hLw0nady/6/embedded/result/
You will notice that when the alert pops up, Angular has not yet interpolated "Should not appear" (it appears in its braces). After you dismiss the alert, however, it disappears.
Example 2 (how to prevent the alert):
An example of hiding the code that "should not run" in a directive
can be viewed here: https://jsfiddle/hLw0nady/4/
In this example, only if you replace ng-if="false"
with ng-if="true"
will you get your alert.
I think that false expression is not well converted to angular false. I can prove this by setting:
<div ng-if="!true">
Which doesn't render text in current div. Anyway, it executes alert, i suppose it is executed before angular runs, that's why.
本文标签:
版权声明:本文标题:javascript - AngularJS ng-if directive briefly renders even when condition is false before removing element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741270216a2369142.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论