我有一个动态表单,从数组中接收数据并迭代它。如果是日期字段,我想以这种格式发送到控制器" dd / MM / yyyy"或者" dd / MM / yyy hh:mm"尝试使用ng-bind
,但控制器接收其他格式,其中包括" 2015年12月23日星期三00:00:00 GMT-0100(本地标准时间)",这应该是格式为通过$http.post
在服务中发送。
这是我的字段输入
仅限日期:
<div class="form-field">
<label>{{fa.title}}</label>
<p class="input-group">
<input type="date" class="form-control"
ng-model="sendForm[fa.name]"
placeholder="dd/MM/yyyy"
ng-bind="sendForm[fa.name] | date:'dd/MM/yyyy'"/>
</p>
</div>
日期和时间:
<div class="form-field">
<label>{{fa.title}}</label>
<p class="input-group">
<input type="datetime-local" class="form-control"
ng-model="sendForm[fa.name]"
placeholder="dd/MM/yyyy hh:mm"
ng-bind="sendForm[fa.name] | date:'dd/MM/yyyy hh:mm'"/>
</p>
</div>
controller.js
app.controller("myAppCtrl", ["$scope", "SendForm", "FORMS", function ($scope, SendForm, FORMS) {
$scope.sendForm = function (form) {
SendForm.sendInfo(form,
function onSuccess() {
$scope.success = true;
},
function onError(message) {
$scope.error = true;
},
function onFinally() {
$scope.clearForm(form);
})
}]);
service.js
app.factory("SendForm", ["$http", "CONFIG", "FORMS","$httpParamSerializer", function ($http, CONFIG, FORMS,$httpParamSerializer) {
return {
sendInfo: function (form, onSuccess, onError, onFinally, message) {
var data = {
absolute: CONFIG.absolute
};
for (var i = 0, length = FORMS.form_variables.length; i < length; i++) {
if (angular.isUndefined(form[FORMS.form_variables[i].name])) {
var key = FORMS.form_variables[i].name;
var value = "";
data[key] = value;
} else {
var key = FORMS.form_variables[i].name;
var value = form[FORMS.form_variables[i].name];
data[key] = value;
}
}
var config = {
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
};
$http.post(CONFIG.urlSendForm, $httpParamSerializer(data), config).success(function () {
onSuccess();
onFinally();
}
}]);
答案 0 :(得分:1)
&#34; date&#34;您正在使用的只是UI中使用的Angualr过滤器,以正确的格式返回/显示日期。它不会改变实际值(只返回格式化的值)。
如果您想在控制器中将日期设为'dd/MM/yyyy hh:mm'
,请按以下方式使用 -
angular.module('myModule')
.controller('myController', ['$filter', '$scope', function($filter, $scope) {
var formattedDate = $filter('date')($scope.sendForm[fa.name], 'dd/MM/yyyy hh:mm');
}]);
答案 1 :(得分:0)
您需要在控制器和服务中格式化日期,以便将此格式化日期发送到服务器吗?你使用哪个角度版本?如果您在服务中识别出值是日期,您可以在工厂中注入$ filter服务并使用它来格式化日期。
在你的工厂
app.factory("SendForm", ["$http", "$filter","CONFIG", "FORMS","$httpParamSerializer", function ($http, $filter,CONFIG, FORMS,$httpParamSerializer) {
return {
sendInfo: function (form, onSuccess, onError, onFinally, message) {
var data = {
absolute: CONFIG.absolute
};
var dateHourRegex = /\d{2}\/\d{2}\/\d{4}\s+\d{2}\:\d{2}/,
onlyDateRegex = /\d{2}\/\d{2}\/\d{4}/;
for (var i = 0, length = FORMS.form_variables.length; i < length; i++) {
if (angular.isUndefined(form[FORMS.form_variables[i].name])) {
var key = FORMS.form_variables[i].name;
var value = "";
data[key] = value;
} else {
var key = FORMS.form_variables[i].name;
var value = form[FORMS.form_variables[i].name];
if(onlyDateRegex.test(value)){
value = dateHourRegex.test(value) ? $filter('date')(value, 'dd/MM/yyyy hh:mm')
: $filter('date')(value, 'dd/MM/yyyy');
}
data[key] = value;
}
}
var config = {
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
};
$http.post(CONFIG.urlSendForm, $httpParamSerializer(data), config).success(function () {
onSuccess();
onFinally();
}
}]);