我有一个动态表单,可以从数组接收数据并对其进行迭代。如果是日期字段,我想以这种格式发送到 Controller “dd/MM/yyyy”或“dd/MM/yyy hh:mm”尝试使用 ng-bind 但 Controller 以其他格式接收“Wed Dec 23 2015 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();
}
}]);
最佳答案
您使用的“日期”只是 UI 中使用的 Angualr 过滤器,用于以正确的格式返回/显示日期。它不会更改实际值(只是返回格式化值)。
如果您想在 Controller 中获取 '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');
}]);
关于javascript - 格式化 View 中的日期输入 Angular JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34222333/