【发布时间】:2014-07-28 08:09:06
【问题描述】:
此 AngularJS 1.2.21 指令用作通用解决方案,用于支持 type="date" 而不支持它的浏览器。
directive('datePicker', function(){
var el = document.createElement('input');
el.setAttribute('type','date');
var typeDateSupport = (el.type === 'date');
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
if (typeDateSupport) {
elm.attr('type', 'date');
elm.attr('placeholder', null);
/* Type date takes care of the right format. The display value is localized, the real value is always YYYY-MM-DD,
so that we do not need to add any parsers and formatters */
} else {
elm.attr('type', 'text');
elm.attr('readonly', 'readonly');
elm.datepicker({
dateFormat: 'yy-mm-dd', // TODO: internationalize this
onSelect: function(date) {
scope.$apply(function() {
ctrl.$setViewValue(date);
});
}
});
if (attrs.hasOwnProperty('max')) {
elm.datepicker('option', 'maxDate', $.datepicker.parseDate('yy-mm-dd', attrs.max));
}
ctrl.$formatters.unshift(function(value) {
if (angular.isUndefined(value)) {
return undefined;
}
return moment.utc(value).tz(Config.timeZone).format('YYYY-MM-DD');
});
ctrl.$parsers.unshift(function(value) {
if (angular.isUndefined(value)) {
return undefined;
}
var tmp = moment.tz(value, Config.timeZone);
ctrl.$setValidity('date', tmp.isValid());
return tmp.isValid()? tmp : undefined;
return value;
});
}
}
};
})
代码导致此错误:Error: [$rootScope:infdig] in IE 11(不支持type="date")。我其实不知道,为什么....
有人可以帮忙吗?
【问题讨论】:
-
如果删除 else 子句中的
scope.$apply调用会发生什么? -
我想通了,原因是解析器。删除它可以解决问题。但我们需要它。
-
酷。确保您回答自己的问题,以便其他人受益:)
-
不,没有解决。我们需要解析器,我不能直接删除它。我会知道为什么会导致问题。
-
对不起,我误会了
标签: angularjs jquery-ui datepicker