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>
我尝试使用AngularJS进行点击方法,但没有任何效果,没有错误消息甚至是console.log。我做错了什么?
答案 0 :(得分:4)
传递给ngClick
的表达式在当前范围内进行评估。这意味着您必须将函数添加到控制器中的$scope
变量:
theApp.controller('MenuSideController', ['$scope', 'CategoryService',
function ($scope, CategoryService){
CategoryService.getList()
.success(function(list){
$scope.list = list;
});
$scope.menuType = function(id) {
// do things here
}
}
]);