简体   繁体   中英

Passing current date over $http.post method angularjs

I'm trying to send curent date to my restapi so I can work with it. I'm assigning and passing parameters over $http.post method like this:

   (function() {
      var app;

      app = angular.module('vinclucms.sales');

      app.controller('FilterContactsListCtrl', [
        '$scope', '$http', function($scope, $http) {
          var nextContactListUpdateFailed, nextContactListUpdateSuccess;
          $scope.selectDate = null;
          $scope.doAction = function() {
            var data;
            data = {
              date: $scope.selectDate
            };
            return $http.post("/sales/lead_contact/", data).then(nextContactListUpdateSuccess, nextContactListUpdateFailed);
          };
          nextContactListUpdateSuccess = function() {
            return ClientNotifications.showNotification("Success", "Contact Leads list page was updated", "success");
          };
          return nextContactListUpdateFailed = function() {
            return ClientNotifications.showNotification("Alert", "Failed to update contact leads list page", "alert");
          };
        }
      ]);

    }).call(this);

Currently is set to null and I'm having problem with that in the rest it's returning None when I debug my view, question is How can I pass current date over $http.post method?

Date time picker field which I'm using to select date

<div class="flex-grid"
          ng-controller="FilterContactsListCtrl">
        <div class="row">
            <div class="cell size-p20 padding10">
                <label>Select Date: *</label>
                <div class="full-size">
                    <div class="input-control text" data-other-days="true"
                         data-week-start="1"
                         data-role="datepicker" date-format="mmmm d, yyyy">
                        <input type="text" ng-model="selectDate" ng-change="doAction()"/>
                        <button class="button">
                            <span class="mif-calendar"></span></button>
                    </div>
                </div>
            </div>
        </div>
    </div>

You may need to do date conversion, can you try as like below,

var data;
data = {
    date: $filter('date')(new Date($scope.selectDate), 'MM/dd/yyyy', 'US')
};

In case of current date:

var data;
data = {
    date: $filter('date')(new Date(), 'MM/dd/yyyy', 'US')
};

Note: Please inject $filter into your controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM