【发布时间】:2014-01-03 03:04:17
【问题描述】:
我有一个使用 jQuery DatePicker 插件的 AngularJS 指令。 我只想使用 DatePicker 的几个月和几年。
在输入标签之前将以下css样式写入html页面时是可能的。
<div>
<style type="text/css">
.ui-datepicker-calendar {
display: none;
}
</style>
<input type="text" ng-model="xMonth" month-year/>
{{ xMonth }}
</div>
但我想设置那个 css inside 指令。尝试使用子句 $('#ui-datepicker-div').toggleClass('.ui-datepicker-calendar', '显示:无'); 但我没有工作。
如何在 Directive 中设置 css?
angular.module('phonecatDirectives', [])
.directive('monthYear', function() {
return {
require: 'ngModel',
link: function(scope, element, attrs) {
var elem = angular.element(element);
elem.datepicker({
dateFormat: "mm-yy",
changeMonth: true,
changeYear: true,
showButtonPanel: true,
maxDate: +0,
onClose: function(dateText, inst) {
var month, year;
month = $(".ui-datepicker-month :selected").val();
year = $(".ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1));
$('#ui-datepicker-div').removeClass('nodays');
},
beforeShow: function(input, inst) {
var dateString, options;
dateString = $(this).val();
options = {};
if (dateString.length > 0) {
options.defaultDate = $.datepicker.parseDate("dd-" +
$(this).datepicker("option", "dateFormat"), "01-" + dateString);
}
// THIS DOESN'T WORK
$('#ui-datepicker-div').toggleClass('.ui-datepicker-calendar',
'display: none');
if ($(input).hasClass('nodays')) {
$('#ui-datepicker-div').addClass('nodays');
} else {
$('#ui-datepicker-div').removeClass('nodays');
$(this).datepicker('option', 'dateFormat', 'mm.yy');
}
return options;
}
});
}
};
});
最好的问候 戴夫
【问题讨论】:
-
你能发布 Fiddle/Plunker 吗?
标签: angularjs angularjs-directive