Pass ng-model attribute for user directive

So, I have a form where I need to use a custom directive. What I need: pass the user model directive.

 <form> <input type="text" ng-model="user.login"> <input type="password" ng-model="user.password"> <span ng-custom-directive ng-model="user.testfield"></span> </form> 

The directive template is as follows:

 <span><input type="checkbox" ng-model="[HERE I NEED user.testfield TO WORK WITH user]"> </span> 

How can I pass the user model for a directive template?

After submitting the form, I need user.testfield be available in $scope.user as:

 console.log($scope.user) { login: 'test', password: 'test', testfield: true|false } 
+5
source share
2 answers

You can solve it in another way plunker

In short:

 scope: { bindedModel: "=ngModel" }, template: '<input type="text" ng-model="bindedModel">' 
+8
source

Well, I found a similar question and solved my problem as follows:

 angular.module("myApp") .directive "ngCustomDirective", () -> restrict: 'A', scope: field: '@', model: '=' template: '<span><input type="checkbox" ng-model="model[field]"></span>' 

And using the directive will:

 <span ng-custom-directive ng-bind-model="user" ng-bind-field="testfield"> </span> 
0
source

Source: https://habr.com/ru/post/1206097/


All Articles