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';
}
}
};
}]);