I am trying to create a directive that will add an ngModel attribute to a tag based on the attribute value. For example:
angular.module('myModule').
directive('myDirective', function() {
return {
link: function(scope, elem, attrs) {
var modelName = 'myPrefix.' + attrs.name;
attrs.$set('ngModel', modelName);
}
};
});
So that this html:
<input name="foo" my-directive></input>
is compiled into
<input name="foo" ng-model="myPrefix.foo" my-directive></input>
It takes the name of the input, attaches a prefix, and sets the ngModel attribute to that value.
When I try to do this in the link function, it seems like the input isn't being registered with the formController, so that form.foo returns undefined.
Is it possible to accomplish what I'm trying to do?
EDIT:
It seems like the ngModel attribute is being set on the HTML, but it is not being registered with the form, or the ngModelController is not being instantiated. If I look at the value of ngModel in the scope, it does not change when I modify the input.