为什么不子元素表现出NG-如果DIV中,当它是真的? [英] Why don't child elements show inside an ng-if div when it's true?
问题描述
我有一个NG-DIV是否如下:
I have an ng-if div as follows:
<div ng-if="colorSelected" ng-cloak>
<h6>My Color</h6>
</div>
在 colorSelected
是真实的,内部头元素一直没有出现。难道我做错了什么?
When colorSelected
is true, the inner header element never shows up. Am I doing something wrong?
推荐答案
使用NG-节目,而不是NG-如果。
Use ng-show instead of ng-if.
NG-如果创建它自己的范围,所以你里面使用的NG-如果将与不同范围的任何范围内的变量。例如...
ng-if creates it's own scope so any scope variables you use inside of an ng-if will be related to a different scope. For example...
<div ng-if='selectedColors'>
{{iAmDefinedInAController}}
</div>
变量'iAmDefinedInAController'将是空的,即使它是在控制器定义的,因为是在NG-如果内创建了一个新的范围。
The variable 'iAmDefinedInAController' will be empty even if it is defined in the controller because a new scope is created inside the ng-if.
NG-显示并没有这样做,因此就使用这一点,你会好到哪里去。
ng-show does not do this, so just use that and you will be good to go.
这篇关于为什么不子元素表现出NG-如果DIV中,当它是真的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!