Date is not being formatted properly as requested; angularjs

I am trying to get the date in formate of dd/MM/yyy and pass it on to my URI, the date is being passed but not in the format requested. Here is my HTML code:

<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';
        })
    }
});

This is my console out put:

Object {date: "2014-07-24"}
/getnewusers?date=2014-07-24

Upvotes: 0

Views: 66

Answers (1)

Thor Jacobsen
Thor Jacobsen

Reputation: 8851

The filter you're using in your markup only modifies the view - not the actual model.

To apply the filter to the date in the contoller, inject $filter and use it:

yApp.controller('NewPlacesCtrl', function($scope,$http,$filter) {
  $scope.fetch= function(){

    var formdata = {'date' : $filter('date')(this.date, 'format') };
    ...
  }
});

Also, using this.date will not be able to reflect in the view - you should use $scope.date in order to do that. And, the markup should then be value="{{ date | date: 'dd/MM/yyyy' }}"

Upvotes: 1

Related Questions