0

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

2
  • Did you try instead {{ 'date' | date: 'dd/MM/yyyy' }} to write: {{ date | date: 'dd/MM/yyyy' }} ? Commented Jul 8, 2014 at 13:06
  • yes I did and It did not work Commented Jul 8, 2014 at 13:09

1 Answer 1

1

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' }}"

Sign up to request clarification or add additional context in comments.

2 Comments

Accept his answer then :-)
Also do you know how would I use filter for Type="month"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.