加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 为什么ng-mouseover不适用于ng-if

发布时间:2020-12-17 17:41:21 所属栏目:安全 来源:网络整理
导读:我正在尝试在具有“ng-if”的图像上使用“ng-mouSEOver”指令并且它不起作用但是如果我使用“ng-show”指令它可以工作,大家能告诉我为什么吗?或者这是一个AngularJS问题? 在AngularJS文档中,我无法阅读有关它的任何内容:https://docs.angularjs.org/api/n
我正在尝试在具有“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}}

小提琴:http://plnkr.co/edit/Wb6bjyJdHj5qoH7fxGFJ?p=info

解决方法

您观察到的行为是由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">

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读