0

I have a directive which takes an event when a change is detected in an input, I would like to create many inputs with the same directive.

For example:

  <body ng-controller="MainCtrl">
    <input ng-model='val'  caret="2"><br/>
    <input ng-model'"val2' caret="2"><br/>
    <input ng-model'"val3' caret="2"><br/>
    <input ng-model'"val4' caret="2"><br/>
     ....
    <input ng-model'"valn' caret="2"><br/>
  </body>

The problem is that the first parameter of scope.$watch is the model name. How can I make the scope.$watch work with dynamic inputs?.

1 Answer 1

1

Change this line

scope.$watch('val', function(newValue, oldValue) {

to

scope.$watch(attrs.ngModel, function(newValue, oldValue) {

and it would work. You put a watch on attribute value for ng-model

see my plunkr http://plnkr.co/edit/U8An4LJwzneZWv2nUNCT?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for your help, but it doesn't print all the models that use the directive. In this example (plnkr.co/edit/aqJzrqAgrJZ6cjdtatlr?p=preview) , it prints Val5 , but the others doesn't print.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.