简体   繁体   中英

How doesn't ng-click work?

It seems there are many questions about why ng-click is not working

It seems straight forward, but it does not work:

<div class="span1 thumbCount" style="margin-top: 20px;">
  <div id="removeThumb" ng-click="delete()" style="margin-left: 30px;">
     <i class="icon-remove" style="color: #A2A251;"></i>
  </div>
</div>

at the scope, I have:

scope.delete= function(){alert ("clicked!")};

but, delete() is never called

As long as you specify that your page/view is using the angular controller where the delete method is defined, this code should work fine

angular.module('app', [])

.controller('BeerCounter', function($scope, $locale) {

  $scope.delete=function(){alert ("clicked!")};  

});

HTML

<body ng-app="app" ng-controller="BeerCounter">
<div >
  <div id="removeThumb"  ng-click="delete()">
     <i>ssss</i>
  </div>
</div>
</body>

Working demo

Check your console tab in browser for other script errors. If other part of your javascript is crashing, that would be the reason.

Perhaps there are some typos in your code. This one works fine. HTML:

<div ng-app="myApp" ng-controller="myCtrl">
  <div class="span1 thumbCount" style="margin-top: 20px;">
    <div id="removeThumb" ng-click="delete()" style="margin-left: 30px;">
       <i class="icon-remove" style="color: #A2A251;"></i>
    </div>
  </div>
</div>

Controller:

angular.module("myApp", []).controller("myCtrl", ["$scope", function($scope){
  $scope.delete = function(){
    alert("clicked!");
  }
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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