【发布时间】:2015-05-15 21:13:41
【问题描述】:
在我的控制器中,在点击事件时,我向页面添加了一个指令,它将能够调用controllerFunc
$scope.addDirective = function(e, instance){
$scope.instance = instance;
$(e.currentTarget.parentElement).append($compile('<my-directive myfunc="controllerFunc($event)" mydata={{instance}}/>')($scope));
}
在我的指令中,我设置它以便在点击事件上调用 controllerFunc(通过 myfunc: &),然后我尝试通过 $event 传递点击事件
app.directive('myDirective',function(){
return {
restrict: 'AE',
scope: {
mydata: '@',
myfunc: "&"
},
template: '<div class="row"><div class="col-4" ng-click="myfunc($event)"></div></div>',
link: function(scope, elem, attrs){
//ommitted
}
}
}
当我点击相关的 div 时,controllerFunc 在控制器中被调用,但事件被称为undefined。
$scope.controllerFunc = function(e){
//called on the click event but e is undefined
}
在这种情况下,有没有办法用ng-click 传递事件(即我用 ng-click 事件向 dom 添加了一个模板?看起来它应该可以工作(因为 click 事件触发函数)但controllerFunc中没有事件
【问题讨论】:
-
试试这个
ng-click="myfunc( { $event: $event} )。它应该可以工作。
标签: angularjs