Material: ng-click not working with md-button unless function is attached to $scope

Created on 30 Nov 2015  路  2Comments  路  Source: angular/material

When binding an object directly to a controller ng-click doesn't work with ng-button ie:

app.controller('ConfigCtrl', function () {
  var config = this;   
  config.nextTab = function (){
    console.log('Doesn't work');
  };
});
<md-button class="md-raised md-cornered md-primary" ng-click="config.nextTab()">Next</md-button>

But this works :

app.controller('ConfigCtrl', function ($scope) {
  $scope.nextTab = function (){
    console.log('Doesn't work');
  };
});
<md-button class="md-raised md-cornered md-primary" ng-click="nextTab()">Next</md-button>

Am I missing something obvious?

Most helpful comment

That's the behavior of AngularJS.

If you want to use the nextTab Function without using $scope you should provide a variable name for your controller.

ng-controller="ConfigCtrl as config"

and then you can use your this.nextTab function as followed -> config.nextTab()

Take a look at http://codetunnel.io/angularjs-controller-as-or-scope/

All 2 comments

That's the behavior of AngularJS.

If you want to use the nextTab Function without using $scope you should provide a variable name for your controller.

ng-controller="ConfigCtrl as config"

and then you can use your this.nextTab function as followed -> config.nextTab()

Take a look at http://codetunnel.io/angularjs-controller-as-or-scope/

Arrghhh.. stupid typo was to blame..

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

chriseyhorn picture chriseyhorn  路  3Comments

pablorsk picture pablorsk  路  3Comments

sbondor picture sbondor  路  3Comments

Dona278 picture Dona278  路  3Comments