Question

I have a directive barFoo that has the following template:

<div>
    ....
    <input ng-model="data">
</div>

This directive is used inside a form! Now, inside this directive I want to do things like

element.find('input').$setValidity('bar-valid', false);

However, this doesn't work. Also the input field doesn't have a name attribute. Is there any way to do this without a 'name' attribute ?

UPDATE: An other solution is to define ngModel on the directive element

<bar-foo ng-model="data.value"></bar-foo>

And rewrite the directive as follows

....
    .directive('barFoo', function () {
        return {
            require: 'ngModel',
            ...
            link: function (scope, element, attrs, ctrl) {
                ctrl.$setValidity('bar-valid', false);
            }
    });
Was it helpful?

Solution

Have a look at this question: AngularJS Form Validation Directive $setValidity on element

I will give you exactly what you need including code

OTHER TIPS

You can call a directive with name="itAmazingName" and use it in directive template, that should solve your problems

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top