我正在尝试使用自己的控制器获取指令:
http://jsfiddle.net/edwardtanguay/xfbgjun5/14/
然而,当我点击按钮时:
var template = '<button ng-click="vm.addItem()">add item</button>'+
'<ul><li ng-repeat="item in vm.items">{{item}}</li></ul>';
它告诉我:
Error: vm.add is not a function
即使我定义controllerAs
和bindToController
:
return {
restrict: 'A',
scope: {
datasource: '=',
add: '&'
},
controller: controller,
controllerAs: 'vm',
bindToController: true,
template: template
};
并从主控制器传入范围方法:
<div item-menu datasource="customers" add="addCustomer()"></div>
为什么它不会将add
识别为vm
上的函数?
ADDENDUM:控制器如下所示:
.controller('mainController', function ($scope) {
$scope.customers = ['First','Second','Third'];
$scope.score = 0;
$scope.addCustomer = function() {
$scope.score++;
}
})
答案 0 :(得分:2)
答案 1 :(得分:0)