Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

angularjs - Why don't child elements show inside an ng-if div when it's true?

I have an ng-if div as follows:

<div ng-if="colorSelected" ng-cloak>
  <h6>My Color</h6>
</div>

When colorSelected is true, the inner header element never shows up. Am I doing something wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use ng-show instead of ng-if.

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>

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-show does not do this, so just use that and you will be good to go.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...