【发布时间】:2016-02-25 13:58:51
【问题描述】:
这是我正在修改的指令的伪代码。 假设我有以下事件处理程序:
<a my-link ng-click="foo()" foo-check="fooCheck"></a>
angular.module('linkModule')
.directive('mylink', function () {
return {
restrict: 'A',
scope: {
fooCheck: '='
},
link: function link ($scope, $element) {
// bar method has some vital function
// for this directive`s functionality and must be run
// ONLY if certain conditions are met.
var bar = function bar () {
console.log('Executing bar');
};
$element.on('click', function (e) {
e.preventDefault();
// Validate something here
var mustContinue = $scope.fooCheck();
if ( mustContinue ) {
bar();
} else {
// Cancel ng-click here
}
});
}
};
});
那么,我的问题是:我可以阻止 ng-click 事件从指令的链接事件侦听器中执行吗?
【问题讨论】:
标签: javascript angularjs event-handling angular-directive