我有一个自定义指令,我指定了控制器。我在控制器中设置的属性可以在模板中进行插值,但功能不响应模板的ng单击。我正在使用角度版本1.3.15。这里是我的问题的归结版本:角度自定义指令控制器没有响应ng-click
myModule.directive('myDirective',function(){
return {
scope:{},
template:
'<div>' +
'<button ng-click="myFn()">click me {{test}}</button>' +
'</div>',
controller:['$scope',function($scope){
$scope.test = "please";
$scope.myFn = function(){
alert('clicked!');
};
}]
};
});
编辑:这里是我的问题的更完整的版本:
module
.provider('PopoverDirectiveBuilder', function() {
this.$get = [function() {
return function directiveBuilder(config) {
var template = config.template;
var controller = config.controller;
return {
restrict: "E",
scope: {},
template:
'<div >' +
template +
'</div>',
controller: controller,
compile: function (tElem, attrs) {
return {
post: function postLink(scope, iElement, iAttrs, controller) {
//add some properties to the controller to handle the directive showing and hiding
}
};
}
};
};
}];
})
.directive('myPopover',['PopoverDirectiveBuilder',function(PopoverDirectiveBuilder){
return PopoverDirectiveBuilder({
template:'<div><button ng-click="myFn()">click me {{test}}</button></div>',
controller:['$scope',function($scope){
$scope.test = "please";
$scope.myFn = function(){
alert('clicked');
};
}]
});
}])
;
我创建一个“酥料饼的建设者”,其中,通过当一个函数一些参数,返回一个指令定义对象。当我在角度batarang(稳定版本)中检查这个时,$ scope.myFn为null,但定义了$ scope.test。
我觉得这与隔离范围有关,但{{test}}内插很好。
适合我:http://plnkr.co/edit/ZIGU4TTCIxP2KF1I56rc?p=preview – 2015-04-02 15:24:12
它工作正常 – mohamedrias 2015-04-02 15:24:29
你必须有一个错误,这看起来很好,开放的开发控制台。 – 2015-04-02 15:25:55