angularjs – 如果父项具有ng-if,则Angular ng-show不起作用
栏目: JavaScript · 发布时间: 7年前
内容简介:翻译自:https://stackoverflow.com/questions/20884786/angular-ng-show-not-working-if-parent-has-ng-if
我有一个视图,其中父div具有ng-if,并且一些子元素具有ng-show.当嵌套在具有ng-if的元素下时,ng-show似乎无法正常工作.这是一个Angular bug还是我做错了什么? See this plunker
.
HTML:
<!-- with ng-if on the parent div, the toggle doesn't work -->
<div ng-if="true">
<div>
visibility variable: {{showIt}}
</div>
<div ng-show="!showIt">
<a href="" ng-click="showIt = true">Show It</a>
</div>
<div ng-show="showIt">
This is a dynamically-shown div.
<a href="" ng-click="hideIt()">Hide it</a>
</div>
</div>
<br/><br/>
<!-- with ng-show on the parent div, it works -->
<div ng-show="true">
<div>
visibility variable: {{showIt}}
</div>
<div ng-show="!showIt">
<a href="" ng-click="showIt = true">Show It</a>
</div>
<div ng-show="showIt">
This is a dynamically-shown div.
<a href="" ng-click="hideIt()">Hide it</a>
</div>
</div>
JavaScript:
scope.hideIt = function () {
scope.showIt = false;
};
谢谢,
安迪
与ng-show不同,ng-if指令创建一个新范围.
因此,如果在子范围内而不是在主范围上设置showIt属性,则在ng内部编写showIt = true.
要修复它,请使用$parent访问父作用域上的属性:
<div ng-if="true">
<div>
visibility variable: {{showIt}}
</div>
<div ng-show="!showIt">
<a href="" ng-click="$parent.showIt = true">Show It</a>
</div>
<div ng-show="showIt">
This is a dynamically-shown div.
<a href="" ng-click="hideIt()">Hide it</a>
</div>
</div>
演示 Plunker .
翻译自:https://stackoverflow.com/questions/20884786/angular-ng-show-not-working-if-parent-has-ng-if
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Google御用網頁語言Node.js
郭家寶 / 佳魁資訊 / 2013-4-26 / NT 490
這是一本 Node.js 的入門教學,寫給想要學習 Node.js,但沒有任何系統的經驗的開發者。如果你聽說過 Node.js,並被它許多神奇的特性吸引,本書就是為你準備的。 透過閱讀本書,你可以對 Node.js 有全面的認識,學會如何用 Node.js 程式設計,了解事件驅動、非同步式 I/O 的程式設計模式,同時還可以了解一些使用JavaScript 進行函數式程式設計的方法。 ......一起来看看 《Google御用網頁語言Node.js》 这本书的介绍吧!