How to remove attribute directives from element directive in AngularJS? -
i have element directive , attribute directive:
<my-element my-attribute="value"></my-element>
my-attribute directive require ngmodel:
app.directive('myattribute', [ function() { var definition = { restrict: 'a', require: 'ngmodel', link: function ($scope, $element, $attrs, ctrl) {...} } return definition;
my-element can used without ng-model template of my-element contains input has ng-model. my-attribute should removed my-element , added input inside my-element. in compile function in my-element directive have:
var value = element.attr('my-attribute'); element.remove('my-attribute'); element.find('input').attr('my-attribute', value);
it works ok when attribute directive doesn't have require: 'ngmodel'. if have ngmodel required get:
error: [$compile:ctreq] controller 'ngmodel', required directive 'myattribute', can't found!
why? how can fix this?
when have require: 'ngmodel'
you'll receive error if don't have ng-model
attribute on element directive on - that's directive property means. says "give me controller of ngmodel's controller available 4th parameter in link function.
to avoid this, if don't need use it, can add ?
sign before ngmodel
, means it's optional:
.... require: '?ngmodel', ....
Comments
Post a Comment