2

I have a custom validation directive that I had attributed to the form

<form myValidations>

app.directive('myValidations', function(){
return{
    //require: 'ngModel', note its commented out

    link: function(s,e,a,ctrl){//note the emphasis on the ctrl


    }
}
});

now how would I grip this ctrl.$parsers if I don't have the 'require: ngModel' to do it for me.

could i find an element and then call its ng-model in some function to return an ng-model-controller? so that I can #$parsers from there?

1
  • can you provide a jsfiddle with your relevant code? Commented Mar 14, 2013 at 0:24

1 Answer 1

4

Answering your question, you can call angular.element('elementSelector').controller('ngModel'). It would return the ngModel the same way a require would.

The question is: if you are creating a new directive, why not use its require feature? There are big chances that you are not using the right solution for your problem. So a bit more of code would give us the opportunity to further enlighten your question.

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

6 Comments

Thanks, you are awesome, Well, I need to contain everything inside one validator directive, and so I would need to perform $parsers on a multiple number of ng-models, so one require I would assume could only perform .controller on the whole form which I attributed the diretive to. Thanks.
Does your validation consider all fields together? Or is it the same for all fields individually? For the first case, you can create a directive that applies to the form and use name property to expose all children controls. For the latter, you can create an agnostic directive that adds a $parser to the element it applies to.
individually, I am checking for individual inputs, which need controller on their own. For this task I was asked to namespace the entire validation into one directive.
A second question is, is there a way I can grep the ng:show param "form.username.$error" from controller or directive?
also adding on to your answer, One problem i've noticed is that if you don't import jquery, jqlite will not be able to do .find (id and class) and .controller properly if the form consist of complex divs with select radio and inputs. I believe you are a big expert on angular, could you please verify this?
|

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.