我正在尝试在具有“ng-if”的图像上使用“ng-mouSEOver”指令并且它不起作用但是如果我使用“ng-show”指令它可以工作,大家能告诉我为什么吗?或者这是一个AngularJS问题?
在AngularJS文档中,我无法阅读有关它的任何内容:https://docs.angularjs.org/api/ng/directive/ngMouseover
NG-秀
<button ng-show="true" ng-mouSEOver="countOne = countOne + 1" ng-init="countOne=0"> Increment (when mouse is over) </button> Count: {{countOne}}
NG-如果
<button ng-if="true" ng-mouSEOver="countTwo = countTwo + 1" ng-init="countTwo=0"> Increment (when mouse is over) </button> Count: {{countTwo}}
解决方法
您观察到的行为是由ngIf如何工作引起的 –
from the docs
Note that when an element is removed using ng-if its scope is destroyed
and a new scope is created when the element is restored. The scope
created within ngIf inherits from its parent scope using prototypal
inheritance. An important implication of this is if ngModel is used
within ngIf to bind to a javascript primitive defined in the parent
scope. In this case any modifications made to the variable within the
child scope will override (hide) the value in the parent scope.
这基本上意味着如果你使用ng-if,你需要使用$parent.像这样:
<button ng-if="true" ng-mouSEOver="$parent.countTwo = countTwo + 1" ng-init="$parent.countTwo=0">