【发布时间】:2018-11-07 11:56:11
【问题描述】:
使用 AngularJS 1.5,我构建了一个自定义属性指令,它向元素添加了一个 date-range-picker 指令及其选项:
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);
}
};
}
};
});
当我将此指令添加到这样的简单输入时:
<input cl-single-date-picker type="text" ng-model="someModel"/>
我会在 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="">
并且日期范围选择器处于活动状态并且正在工作。 但是,如果我向我的元素添加条件指令,在我的情况下 ng-switch-when:
<input ng-switch-when="1" cl-single-date-picker type="text" ng-model="someModel"/>
这个元素根本不会被渲染,即使指令函数正在被执行。如何将我的指令添加到带有条件指令的元素并渲染它?
可行的解决方案(已编辑) 通过添加比 ng-switch 更高的优先级值(即 1200 apparently),customed 指令现在在 ng-switch-then 指令之前执行,并且正在呈现 datepicker。
【问题讨论】:
标签: javascript angularjs angularjs-directive ng-switch angularjs-ng-switch