Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS ng-click not working, but ng-change is, when calling the same function

EDIT The code is all correct, the problem was because of including 'ngTouch', see my own answer below.

I am probably making some stupid mistake here, but for the life of me I cannot find it. I have this piece of markup, which is correctly wired up with a controller:

<input type="text" ng-change="doStuff()" ng-model="stuff"/>
<button ng-click="doStuff()">doStuff</button>

The controller code:

console.log('Hi from controller');
$scope.stuff = "something";
$scope.doStuff = function() {
   alert('doing stuff');
}

The problem is nothing happens when I click the button. If I change the input field, I get the alert, so ng-change works, but ng-click does not. Let me know if this is not enough information, but I would not know what else to provide, and the general setup seems to be working fine...

The rest of the HTML does not contain any Angular directives, and it is loaded like this in myModule.config:

$stateProvider
    .state('stuff', {
            templateUrl: 'pages/stuff.html',
            url: '/stuff',
            controller: 'StuffCtrl'
     })

and the controller is defined like this:

angular.module('myModule')
    .controller('StuffCtrl', function ($scope) {
        // above code
    });
like image 586
Thomas Hirsch Avatar asked Apr 15 '15 15:04

Thomas Hirsch


1 Answers

It turned out that the problem was with another dependency 'ngTouch'. I did not use it, but still it was loaded. My module did not even depend on it. (I am using that admin site template from here: http://startangular.com/product/sb-admin-angular-theme/). After removing loading of the ngTouch it worked as expected. I will file this as a bug to both projects as well... Thanks for your help!

like image 146
Thomas Hirsch Avatar answered Sep 27 '22 22:09

Thomas Hirsch