Warning: This is just an idea that works in a trivial example. I'm not saying it's wrong (this is open to discussion, though) but I have NOT used it in a more complicated context.
So... you can actually create a second input directive and have it applied only when another directive (let's say myDirective) has been applied to the enclosing form.
Suppose you have 2 forms:
<body>
<form name="myForm1" ng-controller="MainCtrl">
Name: <input id="name1" type="text" ng-model="data.name" /><br/>
Surname: <input id="surname1" type="text" ng-model="data.surname" />
<pre>{{data}}</pre>
</form>
<br/>
<form name="myForm2" ng-controller="MainCtrl" my-directive>
Name: <input id="name2" type="text" ng-model="data.name" /><br/>
Surname: <input id="surname2" type="text" ng-model="data.surname" />
<pre>{{data}}</pre>
</form>
</body>
Only the second form is tagged with my-directive. Now, your directives could look like:
app.directive("myDirective", function(){
return {
restrict: 'A',
require: ['form'],
controller: function() {
// nothing here
},
link: function(scope, ele, attrs, controllers){
var formCtrl = controllers[0];
console.log("myDirective applied to form:", formCtrl.$name);
}
};
});
app.directive("input", function(){
return {
restrict: 'E',
priority: -1000,
require: '^?myDirective',
link: function(scope, ele, attrs, ctrl){
if (ctrl) {
console.log("applying custom behaviour to input: ", ele.attr('id'));
// ... awesomeness here
}
}
};
});
See it live and check out the logs. The original input directive lives side-by-side with your own. The proof for that is that the form still works (as you type, the model is updated: that's input's and then, ngModel's job).
Your input directive could also use ngModel to manipulate the input value:
app.directive("input", function(){
return {
restrict: 'E',
priority: -1000,
require: ['?ngModel', '^?myDirective'],
link: function(scope, ele, attrs, ctrls){
var ngModel = ctrls[0];
var myDirective = ctrls[1];
if (myDirective) {
console.log("applying custom behaviour to input: ", ele.attr('id'));
// ... awesomeness here
}
}
};
});