【发布时间】:2015-12-11 11:18:58
【问题描述】:
我有一个动态表单,可以从数组中接收数据并对其进行迭代。如果是日期字段,我想以这种格式发送到控制器“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();
}
}]);
【问题讨论】:
标签: javascript html angularjs date datetime