当我从服务器获取模型时,它看起来像这样:
$scope.m =
{
name: "John",
Dt: "2013-10-03T18:47:33.5049087-07:00"
};
视图看起来像:
<input title="Date" ui-date ng-model="m.Dt" />
我将jQuery datepicker的默认日期格式设置为:
$.datepicker.setDefaults({dateFormat: 'mm-dd-yy'});
尽管输入的初始值仍为“ 2013-10-03T18:47:33.5049087-07:00”。如果我使用日期选择器更改日期,则仅格式化为mm-dd-yy
。
如何获得初始值也要为mm-dd-yy
格式?
您的$ scope.m.Dt属性应为日期类型,而不是字符串。
$scope.m =
{
name: "John",
Dt: new Date()
};
要设置日期格式,请使用ui-date-format指令,例如:
<input title="Date" ui-date ui-date-format="mm-dd-yy" ng-model="m.Dt" />
请参见自述文件中的示例:https://github.com/angular-ui/ui-date#ui-date-format-directive
问题是“日期”只是一个字符串,Angular想要一个本地的Date对象(或时间戳)。
我发现了两种处理方法:立即清除数据或监视变量并重新分配其类型。
[我的首选解决方案是在引入数据时对数据进行处理。由于我知道对象具有date
,因此我仅从$http
success
承诺中清除了JSON数据,如下所示:
$http.get('data.json').success(function(data) {
data.date = Date.parse(data.date);
$scope.model = data;
}
[在将数据分配给$scope
之前会进行转换,因此Angular会认为$scope.model.date
是正确的本机JS Date对象格式。
另一个解决方案是专门$watch
变量的类型。在控制器中的某个位置,添加以下内容:
$scope.$watch('model.date', function() {
if (typeof $scope.model.date === 'string') {
$scope.model.date = Date.parse($scope.model.date);
}
});
每次修改$scope.model.date
时都会检查类型。显然,这会增加开销,但在某些情况下可能会有用。
我有同样的问题。这是我在Angularjs中使用Jquery-UI日历所做的事情
日期格式为“ 2015-03-24T04:00:00”
首先修剪日期字符串以仅获取年,月和日。
var date = "2015-03-24T04:00:00"
var formattedDate = date.match(/[\d-]+/).pop();
// formattedDate is now "2015-03-24" which is passed into
// the directive below as the input.$modelValue.
现在,在您的指令或控制器内执行以下操作...
// Here is directive example.
link: function( scope, element, attrs, input ){
element.datepicker( optionsObjectHere );
setInitialDateFormatOnInput();
function setInitialDateFormatOnInput(){
setTimeout(function(){ // This timeout is required to delay the directive for the input.modelValue to resolve, however, no actual delay occurs!
element.datepicker( "setDate", formatToJqueryUIDateFormat());
});
}
function formatToJqueryUIDateFormat(){
return $.datepicker.parseDate( 'yy-mm-dd', input.$modelValue );
// 'yy-mm-dd' needs to match the input.$modelValue format from above.
}
} // link
这是我在输入中使用整个jquery UI的方式。
HTML
<input type="text" class="inline" ng-model="inputValue" my-calendar-popup="calendarOptions" />
其中calendarOptions是以下对象
var calendarOptions = { minDate: 0, buttonImage: "calendar-icon.png", buttonImageOnly: 'true', showOn: "both", dateFormat: "MM d, yy" };
目录
app.directive('myCalendarPopup', function(){
var defaultOptions = { minDate: 0, buttonImage: "calendar-icon.png", buttonImageOnly: 'true', showOn: "both", dateFormat: "MM d, yy" };
// defaultOptions just in case someone doesn't pass in options.
return {
require:'?ngModel',
restrict: 'A',
link: function( scope, element, attrs, input ){
if ( !input ){ return; } // If no ngModel then return;
element.datepicker( createCalendarOptions());
setInitialDateFormatOnInput();
function createCalendarOptions(){
if( !attrs.rsCalendarPopup ){ return addRequiredJqueryFunction( defaultOptions );}
return formatOptions();
}
function formatOptions() {
var options = scope.$eval( attrs.rsCalendarPopup );
// Turn string into object above.
return addRequiredJqueryFunction( options );
}
function addRequiredJqueryFunction( options ){
options.onSelect = changeDate;
// add onSelect to passed in object and reference local changeDate function, which will update changes to input.$modelValue.
return options;
}
function changeDate( date ){
input.$setViewValue( date );
}
function setInitialDateFormatOnInput(){
setTimeout(function(){
// This timeout is required to delay the directive for the input.modelValue to resolve.
// However, there is no actual timeout time. This is a must to get
// Angular to behave.
element.datepicker( "setDate", formatToJqueryUIDateFormat());
});
}
function formatToJqueryUIDateFormat(){
return $.datepicker.parseDate( 'yy-mm-dd', input.$modelValue );
// 'yy-mm-dd' is not the format you want the calendar to be
// it is the format the original date shows up as.
// you set your actual formatting using the calendar options at
// the top of this directive or inside the passed in options.
// The key is called dateFormat, in this case it's set as
// dateFormat: "MM d, yy" which makes June 30, 2015.
}
} // link
} // return
});
注意:我只能在显示的配置中使它起作用。我已将其添加到控制器,甚至是指令控制器,并且无法重新创建初始日期条件。我还没有找到原因。也许此解决方案仅在嵌入另一个隔离作用域指令中的链接函数内起作用,并且由于特定的时序而起作用。