ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value.
The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
NgModel expects the bound element to have a value property, which div s don't have. That's why you get the No value accessor error. I don't know if the input event is supported on all browsers for contenteditable . You could always bind to some keyboard event instead.
Directives are markers on a DOM element that tell AngularJS to attach a specified behavior to that DOM element or even transform the DOM element and its children. In short, it extends the HTML. Most of the directives in AngularJS are starting with ng- where ng stands for Angular.
EDIT: This answer is old and likely out of date. Just a heads up so it doesn't lead folks astray. I no longer use Angular so I'm not in a good position to make improvements.
It's actually pretty good logic but you can simplify things a bit.
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.model = { name: 'World' };
$scope.name = "Felipe";
});
app.directive('myDirective', function($compile) {
return {
restrict: 'AE', //attribute or element
scope: {
myDirectiveVar: '=',
//bindAttr: '='
},
template: '<div class="some">' +
'<input ng-model="myDirectiveVar"></div>',
replace: true,
//require: 'ngModel',
link: function($scope, elem, attr, ctrl) {
console.debug($scope);
//var textField = $('input', elem).attr('ng-model', 'myDirectiveVar');
// $compile(textField)($scope.$parent);
}
};
});
<body ng-controller="MainCtrl">
This scope value <input ng-model="name">
<my-directive my-directive-var="name"></my-directive>
</body>
.some {
border: 1px solid #cacaca;
padding: 10px;
}
You can see it in action with this Plunker.
Here's what I see:
EDIT As mentioned by Mark in his comment, there's no reason that you can't use ng-model, just to keep with convention.
In general, your directives should use the isolated scope (which you did correctly) and use the '=' type scope if you want a value in your directive to always map to a value in the parent scope.
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', []);
app.controller('MainCtrl', function($scope) {
$scope.name = "Felipe";
$scope.label = "The Label";
});
app.directive('myDirectiveWithScope', function() {
return {
restrict: 'E',
scope: {
ngModel: '=',
},
// Notice how label isn't copied
template: '<div class="some"><label>{{label}}: <input ng-model="ngModel"></label></div>',
replace: true
};
});
app.directive('myDirectiveWithChildScope', function($compile) {
return {
restrict: 'E',
scope: true,
// Notice how label is visible in the scope
template: '<div class="some"><label>{{label}}: <input></label></div>',
replace: true,
link: function ($scope, element) {
// element will be the div which gets the ng-model on the original directive
var model = element.attr('ng-model');
$('input',element).attr('ng-model', model);
return $compile(element)($scope);
}
};
});
app.directive('myDirectiveWithoutScope', function($compile) {
return {
restrict: 'E',
template: '<div class="some"><label>{{$parent.label}}: <input></label></div>',
replace: true,
link: function ($scope, element) {
// element will be the div which gets the ng-model on the original directive
var model = element.attr('ng-model');
return $compile($('input',element).attr('ng-model', model))($scope);
}
};
});
app.directive('myReplacedDirectiveIsolate', function($compile) {
return {
restrict: 'E',
scope: {},
template: '<input class="some">',
replace: true
};
});
app.directive('myReplacedDirectiveChild', function($compile) {
return {
restrict: 'E',
scope: true,
template: '<input class="some">',
replace: true
};
});
app.directive('myReplacedDirective', function($compile) {
return {
restrict: 'E',
template: '<input class="some">',
replace: true
};
});
.some {
border: 1px solid #cacaca;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<div ng-app="model" ng-controller="MainCtrl">
This scope value <input ng-model="name">, label: "{{label}}"
<ul>
<li>With new isolate scope (label from parent):
<my-directive-with-scope ng-model="name"></my-directive-with-scope>
</li>
<li>With new child scope:
<my-directive-with-child-scope ng-model="name"></my-directive-with-child-scope>
</li>
<li>Same scope:
<my-directive-without-scope ng-model="name"></my-directive-without-scope>
</li>
<li>Replaced element, isolate scope:
<my-replaced-directive-isolate ng-model="name"></my-replaced-directive-isolate>
</li>
<li>Replaced element, child scope:
<my-replaced-directive-child ng-model="name"></my-replaced-directive-child>
</li>
<li>Replaced element, same scope:
<my-replaced-directive ng-model="name"></my-replaced-directive>
</li>
</ul>
<p>Try typing in the child scope ones, they copy the value into the child scope which breaks the link with the parent scope.
<p>Also notice how removing jQuery makes it so only the new-isolate-scope version works.
<p>Finally, note that the replace+isolate scope only works in AngularJS >=1.2.0
</div>
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>'
};
});
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>
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.
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 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>',
plunker: http://plnkr.co/edit/9vtmnw?p=preview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With