我有一个基于jQuery UI datepicker的自定义指令:
myApp.directive('uiDatepicker', function ($http, $compile, $parse, InitializationService, Shared, PeriodService)
{
return {
restrict: "E",
replace: true,
transclude: false,
compile: function (element, attrs) {
//...
return function ($scope, element, attrs, controller) {
//...
element.datepicker({
inline: true,
showOtherMonths: false,
dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
onSelect: processChange, //above function
beforeShowDay: renderDay
});
}
}
};
});
在用户点击某个按钮之前,该元素会被隐藏(使用 ng-show ),一旦点击该按钮,就会出现日历。
问题在于,即使隐藏的Angular仍然尝试加载它并调用 compile:部分下指定的函数。
由于在点击按钮之前日历初始化所需的数据不可用,我收到无法读取属性' ...'未定义的
有没有办法告诉Angular只有在没有隐藏指令时加载指令?