ngplayground
ngplayground

Reputation: 21637

AngularJS ng-click and nothing happens

AngularJS

var theApp = angular.module('theApp', []);
theApp.controller('ContentController', ['$scope', function ($scope) {

}]);


theApp.controller('MenuSideController', ['$scope', 'CategoryService', 
    function ($scope, CategoryService){
        CategoryService.getList()
        .success(function(list){
            $scope.list = list;
        });
    }
]);


function menuType(id) {
    console.log(id);
    //do something
}

HTML

<ul>
                        <li ng-repeat="company in list"><a href="#" ng-click="menuType(company.id)">{{ company.name }}</a></li>
                    </ul>

I'm trying to get a click method working using AngularJS but nothing works, there is no error message or even a console.log. What am I doing wrong?

Upvotes: 1

Views: 1506

Answers (1)

Michal Charemza
Michal Charemza

Reputation: 27022

The expression passed to ngClick is evaluated in the current scope. This means you have to add the function to the $scope variable in the controller:

theApp.controller('MenuSideController', ['$scope', 'CategoryService', 
  function ($scope, CategoryService){
    CategoryService.getList()
    .success(function(list){
        $scope.list = list;
    });
    $scope.menuType = function(id) {
      // do things here
    }
  }
]);

Upvotes: 4

Related Questions