我想知道,如果可能的话,扩展Angular的输入指令?我想将一些侦听器附加到页面上的所有输入字段。我认为你可以用$provide.decorate
来装饰现有的模块,但我不知道如何用指令(更准确地说是输入指令)来做到这一点。AngularJS:扩展输入指令
那么,有谁能把我推向正确的方向吗?一些例子?
编辑
这里是我的指导,我到目前为止有:
angular.module('onFocusBlur').
directive('onFocusBlur', ["$rootScope", function($rootScope) {
return {
restrict: "A",
link: function(scope, elem, attrs) {
elem.bind('focus',function() {
scope.$apply(function() {
$rootScope[attrs.onFocusBlur] = true;
});
});
elem.bind('blur',function() {
scope.$apply(function() {
$rootScope[attrs.onFocusBlur] = false;
});
});
}
};
}]);
在我看来,我可以这样将它添加到输入字段:
<input type="email" ng-model="email" on-focus-blur="repositionNavBar">
缺点是,对于每个输入字段,我必须在代码中手动添加此侦听器。因此,改变现有的输入指令将包括这个监听器是很有用的。
你在说什么类型的听众?你能否详细说明一下。 –
[Extensions Angular Directive]的可能的重复(http://stackoverflow.com/questions/17005122/extending-angular-directive) –
我添加了一些代码与我现有的指令 – 23tux