0

Question is simple - why this work like this work?

angular.module('test1', [])
    .directive('myDir', function() {
        return {
            replace:true,
            restrict: 'E',
            template: '<input type="text">',            
        };
    }).directive('nogo', function() {
        return {
            replace:true,
            restrict: 'E',
            template: '<div><input type="text"></div>',            
        };
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="test1">
    <my-dir ng-model="test"></my-dir>
    <nogo ng-model="test"></nogo>
    <input type="text" ng-model="test"/>
</body>

The only difference between two directive is that template for second wrapped with 'div'.

But one work, another doesn't.

Really - I don't understand - why working one is on. But it is.

2 Answers 2

1

the problem is ng-model does not work on div.

change :

.directive('nogo', function() {
        return {
            replace:true,
            restrict: 'E',
            template: '<div><input type="text"></div>',            
        };
    });

to

.directive('nogo', function() {
    return {
        replace:true,
        restrict: 'E',
        template: '<div><input ng-model='test' type="text"></div>',            
    };
});

dont forget to remove ng-model from <nogo>*remove ng-model*</nogo> it is working..

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

1 Comment

I don't get it. ng-model does not work on div? I did NOT ask it for that. Why binding is working in first example - I do NOT any point to ng-model!! Can you point me to documentation, that can explain that? I know about restrict for root-tag on template, but can't link all it together.
0

Your problem here is that you are applying the model to the <div>, here is another question with more information about this problem.

And in this particular case you can change the bind into the template in order to make it work.

angular.module('test1', [])
    .directive('myDir', function() {
        return {
            replace:true,
            restrict: 'E',
            template: '<input type="text">',            
        };
    }).directive('nogo', function() {
        return {
            replace:true,
            restrict: 'E',
            template: '<div><input type="text" ng-model="test"></div>',            
        };
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="test1">
    <my-dir ng-model="test"></my-dir>
    <nogo ng-model="test"></nogo>
    <input type="text" ng-model="test"/>
</body>

Also on the ng-model docs says:

The ngModel directive binds an input,select, textarea (or custom form control)

1 Comment

Thanks, but I know about "input/select/textarea". Main question is - why does first example work? If I made custom-directive, and use ng-model, then it automatically binds to template to special tags ("input/select/textarea")? But if there are two inputs? Then Angular ask me to wrap it with root-tag, right? <DIV>, for example, and default binding will switch off??

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.