dea*_*n44 58 javascript localization angularjs momentjs angular-material
角材料介绍找到了一个新的日期选取器组件在这里.
我希望此组件返回的日期格式为yyy-mm-dd,但我不确定这是如何完成的.通过搜索我发现$mdDateLocaleProvider可以使用,但我找不到使用它的例子.
有人能告诉我一个格式化返回日期的工作示例md-datepicker吗?
Boh*_*rdt 95
$mdDateLocaleProviderAngular Material文档中有一个文档.
angular.module('app').config(function($mdDateLocaleProvider) {
$mdDateLocaleProvider.formatDate = function(date) {
return moment(date).format('YYYY-MM-DD');
};
});
Run Code Online (Sandbox Code Playgroud)
如果您不使用moment.js,请将内部代码替换formatDate为您希望用于格式化日期的任何内容.
这是一个基于Angular Material docs样本的CodePen示例.
小智 58
还要解决kazuar指出的问题:
不幸的是,如果从键盘输入日期,它就不起作用
你也应该定义parseDate方法.来自文档:
$mdDateLocaleProvider.parseDate = function(dateString) {
var m = moment(dateString, 'L', true);
return m.isValid() ? m.toDate() : new Date(NaN);
};
Run Code Online (Sandbox Code Playgroud)
有一个完整的例子,我在我的应用程序中(使用时刻):
$mdDateLocaleProvider.formatDate = function(date) {
return moment(date).format('DD/MM/YYYY');
};
$mdDateLocaleProvider.parseDate = function(dateString) {
var m = moment(dateString, 'DD/MM/YYYY', true);
return m.isValid() ? m.toDate() : new Date(NaN);
};
Run Code Online (Sandbox Code Playgroud)
问候
Ben*_*ros 21
对于那些不使用Moment.js的人,可以格式化为字符串:
.config(function($mdDateLocaleProvider) {
$mdDateLocaleProvider.formatDate = function(date) {
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return day + '/' + (monthIndex + 1) + '/' + year;
};
});
Run Code Online (Sandbox Code Playgroud)
从keyborard输入日期并在启动时返回null以避免在md-datapicker指令中按下"无效日期"时工作得很好:
$mdDateLocaleProvider.formatDate = function(date) {
return date ? moment(date).format('DD/MM/YYYY') : null;
};
$mdDateLocaleProvider.parseDate = function(dateString) {
var m = moment(dateString, 'DD/MM/YYYY', true);
return m.isValid() ? m.toDate() : new Date(NaN);
};
Run Code Online (Sandbox Code Playgroud)
-- 当我们在 md-dialog 中使用 md-DatePicker 时, $mdDateLocaleProvider 服务不会按照我们的需要格式化日期。我们必须在 md-dialog 的控制器中使用 $mdDateLocale 来格式化 md-DatePicker 的日期。例如 -
angular.module('MyApp').controller('AppCtrl', function($scope, $mdDateLocale) {
$mdDateLocale.formatDate = function(date) {
return moment(date).format('YYYY-MM-DD');
};
$scope.myDate = new Date('2015-10-15');
$scope.minDate = new Date();
$scope.maxDate = new Date();
});
Run Code Online (Sandbox Code Playgroud)
使用AngularJS 1.5.9和时刻2.17.1,可以在运行时更改日期格式,月份名称和周名称.
首先配置初始语言.(在此示例中,angular-translate/$ translateProvider的配置是可选的.)
angular.module('app').config(configureLocalization)
configureLocalization.$inject = ['$translateProvider', '$mdDateLocaleProvider', 'localdb', '__config'];
function configureLocalization($translateProvider, $mdDateLocaleProvider, localdb, __config) {
// Configure angular-translate
$translateProvider.useStaticFilesLoader({
prefix: 'locale/',
suffix: '.json'
});
// get the language from local storage using a helper
var language = localdb.get('language');
if (!language || language === 'undefined') {
localdb.set('language', (language = __config.app.defaultLanguage));
}
// Set the preferredLanguage in angular-translate
$translateProvider.preferredLanguage(language);
// Change moment's locale so the 'L'-format is adjusted.
// For example the 'L'-format is DD.MM.YYYY for German
moment.locale(language);
// Set month and week names for the general $mdDateLocale service
var localeData = moment.localeData();
$mdDateLocaleProvider.months = localeData._months;
$mdDateLocaleProvider.shortMonths = moment.monthsShort();
$mdDateLocaleProvider.days = localeData._weekdays;
$mdDateLocaleProvider.shortDays = localeData._weekdaysMin;
// Optionaly let the week start on the day as defined by moment's locale data
$mdDateLocaleProvider.firstDayOfWeek = localeData._week.dow;
// Format and parse dates based on moment's 'L'-format
// 'L'-format may later be changed
$mdDateLocaleProvider.parseDate = function(dateString) {
var m = moment(dateString, 'L', true);
return m.isValid() ? m.toDate() : new Date(NaN);
};
$mdDateLocaleProvider.formatDate = function(date) {
var m = moment(date);
return m.isValid() ? m.format('L') : '';
};
}
Run Code Online (Sandbox Code Playgroud)
稍后您可能有一些基本控制器监视语言变量,当用户选择另一种语言时,该变量会被更改.
angular.module('app').controller('BaseCtrl', Base);
Base.$inject = ['$scope', '$translate', 'localdb', '$mdDateLocale'];
function Base($scope, $translate, localdb, $mdDateLocale) {
var vm = this;
vm.language = $translate.use();
$scope.$watch('BaseCtrl.language', function(newValue, oldValue) {
// Set the new language in local storage
localdb.set('language', newValue);
$translate.use(newValue);
// Change moment's locale so the 'L'-format is adjusted.
// For example the 'L'-format is DD-MM-YYYY for Dutch
moment.locale(newValue);
// Set month and week names for the general $mdDateLocale service
var localeDate = moment.localeData();
$mdDateLocale.months = localeDate._months;
$mdDateLocale.shortMonths = moment.monthsShort();
$mdDateLocale.days = localeDate._weekdays;
$mdDateLocale.shortDays = localeDate._weekdaysMin;
// Optionaly let the week start on the day as defined by moment's locale data
$mdDateLocale.firstDayOfWeek = localeData._week.dow;
});
}
Run Code Online (Sandbox Code Playgroud)
请注意我们不需要更改$mdDateLocale.formatDate和$mdDateLocale.parseDate方法,因为它们已经配置为使用通过调用更改的'L'格式moment.locale(newValue).
有关更多区域设置自定义的信息,请参阅$ mdDateLocaleProvider的文档:https://material.angularjs.org/latest/api/service/ $ mdDateLocaleProvider
额外:这是语言选择器的外观:
<md-select ng-model="BaseCtrl.language" class="md-no-underline">
<md-option
ng-repeat="language in ['en', 'de', 'fr', 'nl']"
ng-value ="language"
><span
class ="flag-icon flag-icon-{{language ==='en' ? 'gb' : language}}"
></span>
</md-option>
</md-select>
Run Code Online (Sandbox Code Playgroud)