NG-CLICK需要双击才能触发

时间:2016-09-28 07:51:13

标签: javascript html angularjs angularjs-ng-click

我的导航栏有一个简单的列表,我使用ng-click和ng-blur来更改列表中的选定选项。不幸的是,它需要双击才能更改,我无法解释原因。

[1]: http://i.stack.imgur.com/6XpP9.png

[]:https://jsfiddle.net/9adtb3rt/1/

JS:

angular.module("myApp", [])
.controller("navCtrl", function(){

});

HTML:

<body ng-app="myApp">
  <div class="navbar" ng-controller="navCtrl">
   <ul>
     <div ng-repeat="e in ['about', 'offers', 'requests']"  ng-init="selected = false" class="{{e}}">
       <a href="#{{e}}" ng-blur="selected=false" ng-click="selected = !selected">
       <li ng-class="{'selected': selected}">{{selected}}</li>
      </a>
    </div>
   </ul>
 </div>
</body>

1 个答案:

答案 0 :(得分:1)

我发现解决方案是我自己的,还有其他div,我在导航栏的href中引用,它与列表中元素的类具有相同的ID。我注意到,如果我从div中删除ID,则ng-click工作正常。谢谢你的时间。

HTML:

<ul>
 <div ng-repeat="e in ['about', 'offers', 'requests']"  ng-init="selected = false">
   <a href="" ng-blur="selected=false" ng-click="selected = !selected; moveTo(e)">
    <li ng-class="{'selected': selected}">{{e | uppercase}}</li>
   </a>
 </div>
</ul>

JS:

$scope.moveTo = function(e){
 var target = $("."+e);
 if(e === "about"){
  $('html, body').animate({
        scrollTop: 0
  }, 1000);
 }else{
  $('html, body').animate({
        scrollTop: target.offset().top
  }, 1000);
 }
};