我试图获取 dd/MM/yyy 格式的日期并将其传递到我的 URI,日期正在传递,但不是所请求的格式。 这是我的 HTML 代码:
<script type="text/ng-template" id="getnewplaces.html" class="users">
<h1>{{message}}</h1>
<form name="myform" id="myform1" ng-submit="fetch()">
<input type="date" ng-model="date" value="{{ 'date' | date: 'dd/MM/yyyy' }}" />
<div>
<center>
<button type="submit" >Fetch</button>
</center>
</div>
</form>
<ul ng-repeat="newUser in newUsers">
<li>{{newUser}} </li>
</ul>
</script>
Angular Controller :
yApp.controller('NewPlacesCtrl', function ($scope, $http) {
$scope.fetch = function () {
var formdata = {
'date': this.date
};
var inserturl = 'http://websitelink/getnewusers?date=' + this.date;
$http.get(inserturl).success(function (data) {
console.log(formdata);
$scope.newUsers = data;
console.log(inserturl);
console.log(data);
$scope.message = 'List of New places';
})
}
});
这是我的控制台输出:
Object {date: "2014-07-24"}
/getnewusers?date=2014-07-24
最佳答案
您在标记中使用的过滤器仅修改 View - 而不是实际模型。
要将过滤器应用于 Controller 中的日期,请注入(inject) $filter
并使用它:
yApp.controller('NewPlacesCtrl', function($scope,$http,$filter) {
$scope.fetch= function(){
var formdata = {'date' : $filter('date')(this.date, 'format') };
...
}
});
此外,使用 this.date
将无法反射(reflect)在 View 中 - 您应该使用 $scope.date
来做到这一点。并且,标记应为 value="{{ date | date: 'dd/MM/yyyy' }}"
关于javascript - 日期格式不符合要求; Angular js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24632572/