What you tried is actually working: see this Plunker
You don't "see" it in the input because changing the model this way doesn't call controller.$render()
to set the new controller.$viewValue
.
But why don't you simply change the $scope
value (unless you don't know it, but it would be weird):
angular.module('main').directive('datepicker', [function() {
return {
require: '?ngModel',
link: function(scope, element, attributes, controller) {
var model = attributes['ngModel'];
scope[model] = 'bar';
}
};
}]);
And in your html:
<input ng-model="yourVariable" datepicker>
EDIT: (dynamic solution)
angular.module('main').directive('datepicker', [function() {
return {
require: '?ngModel',
link: function(scope, element, attributes, controller) {
// get the value of the `ng-model` attribute
var model = attributes['ngModel'];
// update the scope if model is defined
if (model) {
scope[model] = 'bar';
}
}
};
}]);
$scope
value related? For now I've only used$modelValue
to read it. Btw, the fourth attribute of the link function isngModelController
in your case, you should call itcontroller
;) – Dario<input ng-model="anythingModel">
and I dont know the name of the model to use it with scope. and aboutcontroller
call. Take a look at linkthis, how do they call it. But its wrong too, I think the best name probably will bengModelCtrl
– Nates