Skip to content Skip to sidebar Skip to footer

Angularjs - Extending Ngmodel

Afternoon, ngModelController gives you control over $dirty and $pristine but I would like to have my own one. How would I go about extending the ngModelController to offer somethin

Solution 1:

You cannot extend ngModelDirective, but what you can do is create your own ng-model. It's a relatively unknown thing in Angular that you can have multiple directives with the same name, and all of them will be executed. Here's a JSBin with an example: http://jsbin.com/nopoyiso/1/edit

And for those who can't be bothered opening the JSBin:

myApp.directive('ngModel', function() {
  return {
    restrict: 'A', 
    require: '?ngModel',
    link: function(scope, elem, attrs, ngModel) {
      ngModel.setCustom = function() {
        console.log('hello!');
      }
    }
  }
});

So this is a way to do it, but I think you'll end up confusing both yourself and others by doing this. Could you elaborate on what you're trying to do? Perhaps there's a better way to go about it.

Post a Comment for "Angularjs - Extending Ngmodel"