请教一个angularjs中在指令调用controller方法的问题
1个回答
展开全部
你需要把scope.myClick parse成一个function, 然后再调用它。
var fn = $parse(scope.myClick); //parse it as function
fn('123') //call the function.
你可以参照一个rightClick directive 的写法:
全选复制放进笔记.directive("rightclick", ['$parse', function($parse, $scope) {
return {
restrict: 'A',
transclude: true,
scope:{
'rightclick': '&rightclick'
},
link: function(scope, element, attrs) {
element.bind('contextmenu', function(event) {
var fn = $parse(scope.rightclick); //parse it as function
scope.$apply(function() {
event.stopPropagation();
event.preventDefault();
fn();
});
});
}
};
}])
var fn = $parse(scope.myClick); //parse it as function
fn('123') //call the function.
你可以参照一个rightClick directive 的写法:
全选复制放进笔记.directive("rightclick", ['$parse', function($parse, $scope) {
return {
restrict: 'A',
transclude: true,
scope:{
'rightclick': '&rightclick'
},
link: function(scope, element, attrs) {
element.bind('contextmenu', function(event) {
var fn = $parse(scope.rightclick); //parse it as function
scope.$apply(function() {
event.stopPropagation();
event.preventDefault();
fn();
});
});
}
};
}])
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询