简体   繁体   English

从json结果格式化日期并显示在表angularjs中

[英]format date from json result and display in table angularjs

i do a get request to a page that returns a json with dates in format "dd/MM/yyyy". 我向页面返回请求,该页面返回日期格式为“ dd / MM / yyyy”的json。 i parse it and i display it in a table such as below 我解析它并将其显示在如下表中

<script>
angular.module("myApp",[])
.controller("mainController", function($scope, $http) {
    $http.get('backList.php',{params: {dta: $scope.dataSelected}})
            .success(function(data) { if("null" == data) 
                                        $scope.list=""; 
                                    else {
                                        $scope.list=data;
                                    }
                                    } )
            .error(function() { alert("Error retrieve data!"); });
    }
});
</script>

<body ng-controller="mainController">
<div>
<table>
  <tbody>
    <tr ng-repeat="row in list">
      <td align="center">{{row.dta_example}}</td>
      <td align="center"><input class="calendar" type="text" ng-model="row.dta_example" ng-value="row.dta_example" /></td>
    </tr>
  </tbody>
</table>
</div>
</body>

everything works fine but i'd like to use type="data" so that pops-out a calendar but if i use that i get an error that tells me that row.dta_example need to be a Date() object. 一切正常,但是我想使用type =“ data”以便弹出一个日历,但是如果我使用它,则会收到一条错误消息,告诉我row.dta_example必须是Date()对象。

so i did something like this in the success method of the get call: 所以我在get调用的成功方法中做了这样的事情:

.success(function(data) { if("null" == data) 
                                        $scope.list=""; 
                                    else {
                                        $scope.list=data;
                                        var i = 0;
                                        for(var key in $scope.list){
                                            $scope.list[i].dta_example=new Date.exactmatch(key.dta_example)
                                        }
                                    }
                                    } )

but fails. 但失败了。

Try: 尝试:

$scope.list[i].dta_example = new Date(key.dta_example);

Also, 'null' does not = null. 同样,“ null”不等于null。 That test wont work unless your code passes back a string of 'null' somehow. 除非您的代码以某种方式传回字符串“ null”,否则该测试将无法进行。

Use momentjs.com in filter. 在过滤器中使用momentjs.com

<script>
  angular.module("myApp",[])
  .controller("mainController", function($scope, $http) {
     $http.get('backList.php',{params: {dta: $scope.dataSelected}})
        .success(function(data) { if("null" == data) 
                                    $scope.list=""; 
                                else {
                                    $scope.list=data;
                                }
                                })
        .error(function() { alert("Error retrieve data!"); });
      }
   });
  angular.module("myApp").filter('momentFilter', function () {
     return function (date) {
       if (date === null || !angular.isDefined(date))
         return null;
       if (moment(date).isValid()) {
         return moment(date).format('DD. MM. YYYY HH:mm');
       }
       return date;
    }
  });
</script>

<body ng-controller="mainController">
<div>
 <table>
   <tbody>
     <tr ng-repeat="row in list">
       <td align="center">{{row.dta_example | momentFilter }}</td>
       <td align="center"><input class="calendar" type="text" ng-model="row.dta_example" ng-value="row.dta_example" /></td>
     </tr>
   </tbody>
 </table>
</div>
</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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