AngularJS:增加了输入指令 [英] AngularJS: extend input directive
问题描述
我在想,如果有可能,延长角的输入指令?我想一些听众附加到一个页面上的所有输入字段。我想你可以装点 $ provide.decorate
现有的模块,但我不知道如何用指令(还有更多precisely输入指令)来做到这一点。
I was wondering, if it is possible, to extend Angular's input directive? I want to attach some listeners to all input fields on a page. I think you can decorate existing modules with $provide.decorate
, but I have no idea how to do this with a directive (and more precisely the input directive).
所以,任何人都可以把我在正确的方向?一些例子?
So, can anyone push me in the right direction? Some examples?
修改
下面是我的指令,我到目前为止有:
Here is my directive that I have so far:
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;
});
});
}
};
}]);
在我看来,我可以这样像这样添加到输入字段:
In my view, I can add this to an input field like this:
<input type="email" ng-model="email" on-focus-blur="repositionNavBar">
其缺点是,对于每一个输入字段,我要在我的code手动附加监听器。因此,这将是有用的,以改变现有的输入指令,以包括此听众
The downside is, that for every input field, I have to attach this listener manually in my code. Therefore, it would be useful, to alter the existing input directive, to include this listeners.
推荐答案
您可以轻松地扩展任何指令 - 包括输入指令。 这里是添加类指令的一个简单的例子。
You can easily extend any directive - including the input directive. Here is a quick example of adding a class to a directive.
这篇关于AngularJS:增加了输入指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!