2

In my custom directive, I need to update the validity of another input. The directive is something like this :
<customDirective="foo">, in which the value foo is the name of another ngModel.

In my direcitve, I can get its model by :
var foo = scope[attrs.foo];.

But how can I get its ngModelController, to set its validity? Just like this : fooModelController.$setValidity('customDirective', true);

EDIT :

In html, the input is defined as :

<input type="text" name="dateDebut" id="dateDebut" class="form-control" ng-model="formData.dateDebut" customDirective="dateFin" required>
<input type="text" name="dateFin" id="dateFin" class="form-control" ng-model="formData.dateFin" customDirective="dateDebut" required>

I get the dom node by angular.element.find(document.querySelctor('#dateDebut'));

Qianyue
  • 1,767
  • 19
  • 24
  • Why do you need to do this? You are most likely approaching something incorrectly. If you gave a broader context, we may be able to point you in the right direction. – New Dev Mar 16 '15 at 14:53
  • 1
    @NewDev Hello, I have two inputs of dates, a start and an end. I would like to check whether the interval pass 1 week or not. If it is valid, both input should turn to valid. – Qianyue Mar 16 '15 at 14:57
  • $el.controller('ngModel') will give you the ngModelController of $el where $el is the reference of the dom node on which `ng-model` attribute is specified. – Vinay K Mar 16 '15 at 14:59
  • 1
    Probably the most elegant way would be to create a custom input directive that supports `ng-model`: ``, and use 2 individual inputs in its template. Another cheaper approach is to use a `ng-form` over the 2 inputs, which would be invalid if one of the inputs is invalid. – New Dev Mar 16 '15 at 15:00
  • @NewDev Thanks for your suggestion, but the 2 suggestions both seems to be complex for me. Because I have other directives on these input. – Qianyue Mar 16 '15 at 15:10
  • @VinayK I get a undefined exception, when calling $el.controller('ngModel') – Qianyue Mar 16 '15 at 15:40
  • $el should be a jqlite/jquery reference. Can you show the code where `ng-model=foo` is defined? – Vinay K Mar 16 '15 at 15:43
  • Duplicate of http://stackoverflow.com/questions/20982751/custom-form-validation-directive-to-compare-two-fields – Dreamwalker Mar 16 '15 at 15:52
  • @Dreamwalker It's not definitely the same scenario. In that question, it controle just one input. – Qianyue Mar 16 '15 at 16:07

1 Answers1

1

angular.element(document.querySelctor('#dateDebut')).controller('ngModel') -- this will give the ngModelController defined on #dateDebut element.

Here is the plnkr: http://plnkr.co/edit/qXyxEb2QHyhuRUttNMXn?p=preview

Vinay K
  • 5,562
  • 1
  • 18
  • 27