将当前日期传递给$ http.post方法angularjs

时间:2017-04-24 14:03:55

标签: javascript angularjs

我正在尝试将复制日期发送到我的restapi,以便我可以使用它。我正在通过$http.post方法分配和传递参数,如下所示:

   (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);

目前已设置为null,我在调试视图时返回None时遇到问题,问题是如何通过$http.post传递当前日期方法

我用来选择日期的日期时间选择器字段

<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>

1 个答案:

答案 0 :(得分:1)

您可能需要进行日期转换,可以尝试如下,

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

如果是当前日期:

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

注意:请将 $ filter 注入您的控制器