I am trying to create a directive that would create an input field with the same ng-model as the element that creates the directive.
Here's what I came up with so far:
HTML
<!doctype html>
<html ng-app="plunker" >
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css">
<script>document.write("<base href=\"" + document.location + "\" />");</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
This scope value <input ng-model="name">
<my-directive ng-model="name"></my-directive>
</body>
</html>
JavaScript
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = "Felipe";
});
app.directive('myDirective', function($compile) {
return {
restrict: 'E',
scope: {
ngModel: '='
},
template: '<div class="some"><label for="{{id}}">{{label}}</label>' +
'<input id="{{id}}" ng-model="value"></div>',
replace: true,
require: 'ngModel',
link: function($scope, elem, attr, ctrl) {
$scope.label = attr.ngModel;
$scope.id = attr.ngModel;
console.debug(attr.ngModel);
console.debug($scope.$parent.$eval(attr.ngModel));
var textField = $('input', elem).
attr('ng-model', attr.ngModel).
val($scope.$parent.$eval(attr.ngModel));
$compile(textField)($scope.$parent);
}
};
});
However, I am not confident this is the right way to handle this scenario, and there is a bug that my control is not getting initialized with the value of the ng-model target field.
Here's a Plunker of the code above: http://plnkr.co/edit/IvrDbJ
What's the correct way of handling this?
EDIT: After removing the ng-model="value"
from the template, this seems to be working fine. However, I will keep this question open because I want to double check this is the right way of doing this.
it' s not so complicated:
in your dirctive, use an alias: scope:{alias:'=ngModel'}
.directive('dateselect', function () {
return {
restrict: 'E',
transclude: true,
scope:{
bindModel:'=ngModel'
},
template:'<input ng-model="bindModel"/>'
}
in your html, use as normal
<dateselect ng-model="birthday"></dateselect>
This is a little late answer, but I found this awesome post about NgModelController
, which I think is exactly what you were looking for.
TL;DR - you can use require: 'ngModel'
and then add NgModelController
to your linking function:
link: function(scope, iElement, iAttrs, ngModelCtrl) {
//TODO
}
This way, no hacks needed - you are using Angular's built-in ng-model
I took a combo of all answers, and now have two ways of doing this with the ng-model attribute:
var app = angular.module('model', []);_x000D_
_x000D_
app.controller('MainCtrl', function($scope) {_x000D_
$scope.name = "Felipe";_x000D_
$scope.label = "The Label";_x000D_
});_x000D_
_x000D_
app.directive('myDirectiveWithScope', function() {_x000D_
return {_x000D_
restrict: 'E',_x000D_
scope: {_x000D_
ngModel: '=',_x000D_
},_x000D_
// Notice how label isn't copied_x000D_
template: '<div class="some"><label>{{label}}: <input ng-model="ngModel"></label></div>',_x000D_
replace: true_x000D_
};_x000D_
});_x000D_
app.directive('myDirectiveWithChildScope', function($compile) {_x000D_
return {_x000D_
restrict: 'E',_x000D_
scope: true,_x000D_
// Notice how label is visible in the scope_x000D_
template: '<div class="some"><label>{{label}}: <input></label></div>',_x000D_
replace: true,_x000D_
link: function ($scope, element) {_x000D_
// element will be the div which gets the ng-model on the original directive_x000D_
var model = element.attr('ng-model');_x000D_
$('input',element).attr('ng-model', model);_x000D_
return $compile(element)($scope);_x000D_
}_x000D_
};_x000D_
});_x000D_
app.directive('myDirectiveWithoutScope', function($compile) {_x000D_
return {_x000D_
restrict: 'E',_x000D_
template: '<div class="some"><label>{{$parent.label}}: <input></label></div>',_x000D_
replace: true,_x000D_
link: function ($scope, element) {_x000D_
// element will be the div which gets the ng-model on the original directive_x000D_
var model = element.attr('ng-model');_x000D_
return $compile($('input',element).attr('ng-model', model))($scope);_x000D_
}_x000D_
};_x000D_
});_x000D_
app.directive('myReplacedDirectiveIsolate', function($compile) {_x000D_
return {_x000D_
restrict: 'E',_x000D_
scope: {},_x000D_
template: '<input class="some">',_x000D_
replace: true_x000D_
};_x000D_
});_x000D_
app.directive('myReplacedDirectiveChild', function($compile) {_x000D_
return {_x000D_
restrict: 'E',_x000D_
scope: true,_x000D_
template: '<input class="some">',_x000D_
replace: true_x000D_
};_x000D_
});_x000D_
app.directive('myReplacedDirective', function($compile) {_x000D_
return {_x000D_
restrict: 'E',_x000D_
template: '<input class="some">',_x000D_
replace: true_x000D_
};_x000D_
});
_x000D_
.some {_x000D_
border: 1px solid #cacaca;_x000D_
padding: 10px;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>_x000D_
<div ng-app="model" ng-controller="MainCtrl">_x000D_
This scope value <input ng-model="name">, label: "{{label}}"_x000D_
<ul>_x000D_
<li>With new isolate scope (label from parent):_x000D_
<my-directive-with-scope ng-model="name"></my-directive-with-scope>_x000D_
</li>_x000D_
<li>With new child scope:_x000D_
<my-directive-with-child-scope ng-model="name"></my-directive-with-child-scope>_x000D_
</li>_x000D_
<li>Same scope:_x000D_
<my-directive-without-scope ng-model="name"></my-directive-without-scope>_x000D_
</li>_x000D_
<li>Replaced element, isolate scope:_x000D_
<my-replaced-directive-isolate ng-model="name"></my-replaced-directive-isolate>_x000D_
</li>_x000D_
<li>Replaced element, child scope:_x000D_
<my-replaced-directive-child ng-model="name"></my-replaced-directive-child>_x000D_
</li>_x000D_
<li>Replaced element, same scope:_x000D_
<my-replaced-directive ng-model="name"></my-replaced-directive>_x000D_
</li>_x000D_
</ul>_x000D_
<p>Try typing in the child scope ones, they copy the value into the child scope which breaks the link with the parent scope._x000D_
<p>Also notice how removing jQuery makes it so only the new-isolate-scope version works._x000D_
<p>Finally, note that the replace+isolate scope only works in AngularJS >=1.2.0_x000D_
</div>
_x000D_
I'm not sure I like the compiling at link time. However, if you're just replacing the element with another you don't need to do that.
All in all I prefer the first one. Simply set scope to {ngModel:"="}
and set ng-model="ngModel"
where you want it in your template.
Update: I inlined the code snippet and updated it for Angular v1.2. Turns out that isolate scope is still best, especially when not using jQuery. So it boils down to:
Are you replacing a single element: Just replace it, leave the scope alone, but note that replace is deprecated for v2.0:
app.directive('myReplacedDirective', function($compile) {
return {
restrict: 'E',
template: '<input class="some">',
replace: true
};
});
Otherwise use this:
app.directive('myDirectiveWithScope', function() {
return {
restrict: 'E',
scope: {
ngModel: '=',
},
template: '<div class="some"><input ng-model="ngModel"></div>'
};
});
You only need ng-model when you need to access the model's $viewValue or $modelValue. See NgModelController. And in that case, you would use require: '^ngModel'
.
For the rest, see Roys answer.
Since Angular 1.5 it's possible to use Components. Components are the-way-to-go and solves this problem easy.
<myComponent data-ng-model="$ctrl.result"></myComponent>
app.component("myComponent", {
templateUrl: "yourTemplate.html",
controller: YourController,
bindings: {
ngModel: "="
}
});
Inside YourController all you need to do is:
this.ngModel = "x"; //$scope.$apply("$ctrl.ngModel"); if needed
I wouldn't set the ngmodel via an attribute, you can specify it right in the template:
template: '<div class="some"><label>{{label}}</label><input data-ng-model="ngModel"></div>',
Creating an isolate scope is undesirable. I would avoid using the scope attribute and do something like this. scope:true gives you a new child scope but not isolate. Then use parse to point a local scope variable to the same object the user has supplied to the ngModel attribute.
app.directive('myDir', ['$parse', function ($parse) {
return {
restrict: 'EA',
scope: true,
link: function (scope, elem, attrs) {
if(!attrs.ngModel) {return;}
var model = $parse(attrs.ngModel);
scope.model = model(scope);
}
};
}]);
Source: Stackoverflow.com