最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angularjs - ng-click not firing - Stack Overflow

programmeradmin4浏览0评论

$scope.testing is being added as the button Id correctly but clicking the button doesn't fire the alert.

See example -

CONTROLLER

var app = angular.module('StockCategory', []);

app.controller('stockCategoryController', function($scope) {
  $scope.testing = 'World';

  $scope.saveCategory = function() {

    alert('hello');
  }
});

HTML

<div class="stock-categories" ng-app="StockCategory" ng-controller="stockCategoryController">
    <button class="btn save-cat" id="{{testing}}" ng-­click="saveCategory();">Save</button>
  </div>

$scope.testing is being added as the button Id correctly but clicking the button doesn't fire the alert.

See example - http://plnkr.co/edit/RtidzgiUI7ZAMOTu3XHy?p=preview

CONTROLLER

var app = angular.module('StockCategory', []);

app.controller('stockCategoryController', function($scope) {
  $scope.testing = 'World';

  $scope.saveCategory = function() {

    alert('hello');
  }
});

HTML

<div class="stock-categories" ng-app="StockCategory" ng-controller="stockCategoryController">
    <button class="btn save-cat" id="{{testing}}" ng-­click="saveCategory();">Save</button>
  </div>
Share Improve this question edited Aug 6, 2014 at 5:45 garethb asked Aug 6, 2014 at 4:38 garethbgarethb 4,0416 gold badges35 silver badges55 bronze badges 2
  • that's really weird not working in plunker or visual sudio – Nerdroid Commented Aug 6, 2014 at 5:48
  • Should be working, all code looks good. Really weird... – Fizzix Commented Aug 6, 2014 at 5:51
Add a comment  | 

2 Answers 2

Reset to default 14

The problem is that the button element in your plunker has an extra - in your ng-click directive. You can't see it in your plunker, try copy and pasting it in an unformatted text editor like notepad. You will see the extra - that looks like this:

<button class="btn save-cat" id="{{testing}}" ng--­click="doClick()">Save</button>

Possible cause would probably be because of copy and pasting code from an html page to another page or from a different document format to an html format.

To solve this, simply create another button element with the same content, don't copy and paste that button element above.

In my DEMO you will see two buttons with the same markup, the first one doesn't work while the other one works perfectly.

Seems to work fine on this plunker.

I had to make a new one since you were editing the one you posted. It did not look like the code in your question.

Your plnkr had

ng-­click="saveCategory"

That does not work because ng-click expects an expression, not a reference. You had it right in your question.

The code, as it is in your question here works fine.

发布评论

评论列表(0)

  1. 暂无评论