Using AngularJS 1.5, I've built a custom attribute directive, that adds a date-range-picker directive, and it's options, to the element :
app.directive('clSingleDatePicker', function($compile) {
var opts_single_date = {
singleDatePicker: true
};
return {
restrict: 'A',
compile: function compile(element, attrs) {
element.attr('date-range-picker', '');
element.attr('options', 'opts');
element.removeAttr("cl-single-date-picker"); //remove the attribute to avoid indefinite loop
return {
pre: function preLink(scope, iElement, iAttrs, controller) {
},
post: function postLink(scope, iElement, iAttrs, controller) {
scope.opts = opts_single_date;
$compile(iElement)(scope);
}
};
}
};
});
When I'm adding this directive to a simple input like this:
<input cl-single-date-picker type="text" ng-model="someModel"/>
I'll get the required result in the DOM:
<input type="text" ng-model="someModel" class="ng-pristine ng-valid ng-scope ng-isolate-scope ng-empty ng-touched" date-range-picker="" options="opts" style="">
And the date-range-picker is active and doing it's work. However, If I'm adding a conditional directive to my element, In my case ng-switch-when:
<input ng-switch-when="1" cl-single-date-picker type="text" ng-model="someModel"/>
This element will not be rendered at all, even though, the directive function is being executed. How can I add my directive to an element with a conditional directive on it and have it rendered?
A working solution (Edited) By adding a priority value higher than the ng-switch (which is 1200 apparently), The customed directive is now being executed before the ng-switch-then directive, and the datepicker is being rendered.