2015-11-02 53 views
0

我做了一個請求返回一個頁面,返回一個日期格式爲「dd/MM/yyyy」的json。 我分析它和我在一個表中顯示它如下面json結果的格式日期和顯示在表中angularjs

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

一切工作正常,但我想使用類型=「數據」,使彈出出一個日曆,但如果我使用,我得到的一個錯誤告訴我row.dta_example需要是一個Date()對象。

,所以我沒有在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) 
             } 
            } 
            }) 

但失敗。

+0

僅有約日期評論 - 我永遠不會再使用比momentjs其他任何東西,它會讓你的生活變得更輕鬆 - http://momentjs.com – Chris

回答

0

嘗試:

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

而且, '空' 不= NULL。除非你的代碼以某種方式返回了一個'​​null'字符串,否則這個測試不會起作用。

+0

這是我在說什麼它的失敗。 ... – Manuel

0

在過濾器中使用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> 
+0

我添加到您的代碼ng-value =「row.dta_example | momentFilter」並將格式修改爲時間(日期).format('DD/MM/YYYY');但stil不正確。如果我把一個像2012年12月12日這樣的日期,我會得到這個警告「指定的值」12/12/1902「不符合要求的格式,」yyyy-MM-dd「」。和row.dta_example填充,如「2012年12月12日星期三.......」,我希望它是因爲我張貼如此「2012年12月12日」 – Manuel