Reputation: 2028
I builded different directives for entering data (like calendars, colo picker, ...). I would like to give this elements the same behaviour, which the commmon input elmenets have (pristine, dirty). Is there a way to realize that (maybe through inheritance or similar)? If not, anyone has a good idea how to code that proper, so it fits well in well?
Upvotes: 1
Views: 1253
Reputation: 22933
If your directive doesn't use ng-model
internally you have to create that yourself. So if your directive just wraps a textfield that is editable and has some extra features, it's a good idea to still use ng-model
since it gives you those extra things. So something like this:
The HTML:
<my-input my-model="model1">
And the JS:
myModule.directive('myInput', function() {
return {
replace: true,
restrict: 'E',
scope: {
model: '=myModel'
},
template: '<div><input type="text" ng-model="model"</div>',
link: function($scope, elem, attrs) {
// Add extra features to the input
}
};
});
With this, your directive automatically gets the pristine/dirty behavior from using ng-model
. If this isn't an option for you, I'd suggest that you look through the source for NgModelController, specifically the parts about pristine/dirty.
Upvotes: 3