Datepicker值转换为

Datepicker值转换为

我试图将datepicker值转换为字符串或可用格式,以作为参数传递给get方法。然而,
我尝试过的get方法总是出现错误,将datepicker值转换为字符串的最佳方法是什么?



<div ng-app="getserviceApp" ng-controller="getserviceCtrl">
        <button ng-click="FunctionLoadData()">Load Data</button>

        <input type="date" ng-model="from_date" />
        <input type="date" ng-model="to_date" />







<script type="text/javascript">

        var app = angular.module('getserviceApp', [])

        app.controller('getserviceCtrl', function ($scope, $http) {

            var FromDate = new Date($scope.from_date.selectedDate);
            var ToDate = new Date($scope.from_date.selectedDate);

最佳答案

ng模型和$ scope变量的名称存在一些差异。

在您的控制器中,您应该有类似

   app.controller('getserviceCtrl', function ($scope, $http) {
     $scope.from_date = new Date();
     $scope.to_date = new Date();

     $scope.loadData = function(){
       console.log($scope.from_date.toISOString().split('T')[0])
       console.log($scope.to_date.toISOString().split('T')[0])
       //prints the dates in yyyy-MM-dd format
     }
   }


在您的HTML中

<div ng-app="getserviceApp" ng-controller="getserviceCtrl">
    <button ng-click="loadData()">Load Data</button>

    <input type="date" ng-model="from_date" />
      value = {{from_date | date: "yyyy-MM-dd"}}//Display the date
    <input type="date" ng-model="to_date" />
      value = {{from_date | date: "yyyy-MM-dd"}}
</div>

关于javascript - Datepicker值转换为Angularjs中的字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60093069/

10-09 21:53