简体   繁体   English

Angular js ng-click在子元素中不起作用

[英]Angular js ng-click doesn't work in child element

I'm trying to toggle a variable on click of an element in the DOM and I'm getting some strange behaviour. 我试图在点击DOM中的元素时切换变量,我得到了一些奇怪的行为。

Full Example Here 完整示例在这里

Essentially if I put the ng-click on the .options div and leave the controller on the .options-tab div, the event triggers (but applies to everything inside the .options div). 基本上,如果我将ng-click放在.options div并将控制器保留在.options-tab div上,则事件会触发(但适用于.options div中的所有内容)。 And for some reason I am forced to apply the ng-controller again. 由于某种原因,我被迫再次应用ng-controller。

<div ng-app ng-controller="Swipe" class="container">
    <div class="options" ng-click="swiped=!swiped2">
         <div ng-controller="Swipe" class="options-tab" ></div>
    </div>
</div>

If I put it on the element that I want it on, it doesn't trigger the event. 如果我把它放在我想要的元素上,它就不会触发事件。

<div ng-app ng-controller="Swipe" class="container">
    <div class="options">
         <div ng-controller="Swipe" class="options-tab" ng-click="swiped=!swiped2"></div>
    </div>
</div>

You had a few issues: 你有几个问题:

  1. Multiple controller declarations creating duplicate scopes 多个控制器声明创建重复的范围
  2. ng-click="swiped1=!swiped1" on both the options and options-tab elements (it was being set and then reversed immediately) optionsoptions-tab元素上的ng-click="swiped1=!swiped1" (它被设置然后立即反转)
  3. Your 2nd example was set to ng-click="swiped=!swiped2" instead of ng-click="swiped2=!swiped2" 你的第二个例子设置为ng-click="swiped=!swiped2"而不是ng-click="swiped2=!swiped2"

Updated working fiddle: http://jsfiddle.net/Xya3f/2/ 更新了工作小提琴: http//jsfiddle.net/Xya3f/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM