Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: Two way data binding fails if element has ngModel and a custom directive

I made two directives to setup custom error messages in AngularJS:

errors -> displays the error messages for a form

error-message -> sets up a custom error message on an input

For some reason whenever I add the error-message directive to the element the ng-model binding no longer works (but validation does).

See here http://jsfiddle.net/apohl/A8Vgk/111/

Help please :)

like image 753
apohl Avatar asked Mar 14 '13 20:03

apohl


1 Answers

The problem is that your errorMessage directive uses an isolate scope. Isolate scopes affect the entire element, so the ngModel directive was being evaluated in an isolate scope - which obviously can't work - the model is on the parent scope.

I'm not sure why you established an isolate scope here. Since you are trying to create a component that must interact with other directives, an isolate scope is not the best choice. Since errorMessage doesn't affect the current scope, you may not need any defined scope, but you could use a child scope if you so chose.

Take a look here to read more about when to use each type of scope on a directive.

like image 87
Josh David Miller Avatar answered Sep 28 '22 05:09

Josh David Miller