问题
I am using the md-datepicker directive from angular-material, however i would like to typ in the date, not only choose it from the datepicker. I found the following example code:
angular.module('MyApp')
.controller('AppCtrl', function($scope) {
$scope.myDate = new Date();
$scope.minDate = new Date(
$scope.myDate.getFullYear()-1,
$scope.myDate.getMonth(),
$scope.myDate.getDate());
$scope.maxDate = new Date(
$scope.myDate.getFullYear()+1,
$scope.myDate.getMonth(),
$scope.myDate.getDate()); })
.config(function($mdDateLocaleProvider) {
$mdDateLocaleProvider.formatDate = function(date) {
return date ? 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);
};
});
my problem now is where to use the .config section in a MEANJS yeoman generator application. I couldnt find anything. Does someone did this so far?
回答1:
If you're having trouble loading the md-datepicker files and get it to be loaded into MEAN.JS check out this answer.
However if you are already correctly loading your files into your app and you just want to access the config block of the app you can do so by accessing the file located in /modules/core/client/app/init.js, and in this code block just add the logic needed:
// Setting HTML5 Location Mode
angular
.module(app.applicationModuleName)
.config(bootstrapConfig);
function bootstrapConfig($compileProvider, $locationProvider, $httpProvider, $mdDateLocaleProvider) {
$locationProvider.html5Mode(true).hashPrefix('!');
$httpProvider.interceptors.push('authInterceptor');
// Disable debug data for production environment
// @link https://docs.angularjs.org/guide/production
$compileProvider.debugInfoEnabled(app.applicationEnvironment !== 'production');
// md-datepicker configuration
$mdDateLocaleProvider.formatDate = function(date) {
return date ? 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);
};
}
bootstrapConfig.$inject = ['$compileProvider', '$locationProvider', '$httpProvider', '$mdDateLocaleProvider'];
Don't forget to inject $mdDateLocaleProvider in the config block.
来源:https://stackoverflow.com/questions/39568360/setting-another-date-forma-in-meanjs-with-md-datepicker