Artur Kasperek
Artur Kasperek

Reputation: 645

Angular ng-model controller work correct for attribute but for element dont

I have this HTML code:

        <div ng-repeat="(name, input) in loginCtrl.loginModel">
        <input type="{{input.type}}" name="{{name}}" ng-model="input.model" placeholder="{{name | translate}}" ng-minlength="5" input-status required/>
       <!-- <input-status ng-model="input.model"></input-status> -->
    </div>

Code of input-status looks like this :

        .directive('inputStatus', function() {
        return {
            restrict: 'AE',
            templateUrl: 'templates/inputStatus2.html',
            require: 'ngModel',
            link: function($scope, element, attrs, ctrl) {
                ctrl.$parsers.push(function(viewVal) {
                    ctrl.$validate();
                    console.log(viewVal);
                });
            }
        }
    });

When I add to attribiute code of model controller in link function is executed every model change, but when I delete this attribiute and unccomment code, model controller parser function is not executed after every change of input.model. Why ? How to fix this ?

Upvotes: 0

Views: 59

Answers (1)

Matteo Gabriele
Matteo Gabriele

Reputation: 164

You can't remove the directive attribute from the input tag, uncomment the directive element and pretend that the directive will work as it was attached to an input tag.

When you deal with directive as attribute, you already have the html that you need: it's the tag itself, the input tag in your example.

With directive as element, you have to create the element to render in the view attaching a template that will have one main HTML node: a root element, in this case an input tag.

Upvotes: 1

Related Questions