我希望create指令检查当前元素的类,如果没有禁用它,则调用一个函数。
但它不起作用,我看不出任何错误。
app.directive("clickIfEnabled", ['$parse', '$rootScope', function ($parse, $rootScope) {
return {
restrict: 'A',
link: function ($element, attr) {
// We expose the powerful $event object on the scope that provides access to the Window,
// etc. that isn't protected by the fast paths in $parse. We explicitly request better
// checks at the cost of speed since event handler expressions are not executed as
// frequently as regular change detection.
var eventname = "click-if-enabled";
var fn = $parse(attr.attr[eventname], /* interceptorFn */ null, /* expensiveChecks */ true);
return function ngEventHandler(scope, element) {
element.on("click", function (event) {
var callback = function () {
fn(scope, { $event: event });
};
//if (forceAsyncEvents[eventName] && $rootScope.$$phase) {
// scope.$evalAsync(callback);
//} else {
scope.$apply(callback);
// }
});
};
}
};
}]);
演示:http://jsfiddle.net/SbrKj/116/ 只是应该显示警报(“做某事”)