Use ui-validate inside a directive template

487 views
Skip to first unread message

Rafael Dipold

unread,
Feb 13, 2014, 8:16:13 AM2/13/14
to ang...@googlegroups.com
It´s possible use ui-validate inside a directive template?

Original question:
http://stackoverflow.com/questions/21753944/use-ui-validate-inside-a-directive-template



I'll be very grateful if anyone can help me...

Sander Elias

unread,
Feb 13, 2014, 10:49:30 AM2/13/14
to ang...@googlegroups.com
Hi Rafael,

have a look at this plunk. it will work.
ngModel is very powerful, but it's also very picky on its surroundings. Due to the transclude and the isolate scope, it does not register itself in the form.
but for ui-validate, it is enough to get the ng-controller itself. No need to register in a form.

Regards
Sander

Rafael Dipold

unread,
Feb 13, 2014, 11:09:55 AM2/13/14
to ang...@googlegroups.com
Thank you Sander!

The line:
var ctrl = elm.find('input').data('$ngModelController');
 
was sufficient to resolve the problem.

Works like a charm now.

Best Regards!

Sander Elias

unread,
Feb 13, 2014, 11:24:16 AM2/13/14
to ang...@googlegroups.com
Hi Rafael,

I know that that would do, however your solution adds an extra ng-model, that's not used at all. this is just wasting a lot of cpu cycles. Hence the extra changes I put in.

Regards
Sander

Rafael Dipold

unread,
Feb 13, 2014, 11:36:47 AM2/13/14
to ang...@googlegroups.com
Hi Sander,

Thanks for the explanation. I see now, and the punkler is a snippet of my code, but I will adapt it to my reality.

Best Regards

Rafael Dipold
dip...@gmail.com



--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/cMgxWTM-j00/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.

Chris Trude

unread,
May 19, 2014, 1:40:51 PM5/19/14
to ang...@googlegroups.com
I don't understand how his example is going to utilize the isValid function where you are appending the controller to that variable. I'm trying a similar thing but instead of jamming an expression directly into the ui-validate directive call, I'm having it call a function that checks if another field is set as well as the current field, to have it be valid or invalid. 

 
 In my template Directive-
    <select ng-if="novalue" data-ng-model="model" id="{{for}}" name="{{for}}" ng-options="option for option in options | orderBy:'toString()'" placeholder="{{placeholder}}" class="form-control input-sm" ng-change="changed(model)" ui-validate="{noACM : 'checkAcm($value)'}">
            <option value="">— Select —</option>
    </select>
 
In my link function on that template directive
 $scope.checkAcm = function($value){
              if ($scope.model.acm && _.size($($value).text().trim())){
                 return true;
              } else{
                return false;
              }
            }

My current problem is that the checkAcm function never gets called. How in my example do I utilize var ctrl = elm.find('input').data('$ngModelController');

-Chris

Sander Elias

unread,
May 20, 2014, 2:52:46 AM5/20/14
to ang...@googlegroups.com
Chris,

From the code you posted I can't say what goes wrong. If your validation function does not get called, you have a different problem.
Personally I never used ui-valid, so this is too little for me to go on. If you would build a plunk/fiddle/jsbin illustrating your problem,
I will happily take a shot at it!

Regards
Sander
Reply all
Reply to author
Forward
0 new messages