Accessing the $ error in the Angular directive

I have input wrapped in a directive with a dynamic name like this:

<input class="input-field" name="{{name}}" type="number" ... />

Now I want to access the $errorform variable In the directive. Something like that form.{{name}}.$error.number.

Is there any way to do this?

+4
source share
2 answers

If you want to access the form (that is, in the parent area), you need to pass the form to your directive. To do this, you need to indicate that you want to bind in two directions (using =) when you define your directive.

Take a look at https://docs.angularjs.org/guide/directive , more precisely, the area selection part will probably help you.

0

, , $error ngModelController?

return {
  template: '<input ng-model="value" type="number" /><span ng-bind="error | json"></span>',
  scope : {},
  link: function(scope, elem, attr) {
    scope.error = elem.find('input').controller('ngModel').$error;
  }
}

http://plnkr.co/edit/wzuWT1lVevCLHkLLBIAT?p=preview

0

All Articles