2016-03-08 119 views
0

我想實現一個自定義過濾器。我得到了angularjs的依賴錯誤。請幫我解決這個問題。無法理解angulajs依賴性錯誤

下面是我的代碼...

angular.module('Test', []) 
 
    .controller('TestController', ['$scope', function ($scope) { 
 
     $scope.myDate = 1456106575956; 
 
    }]) 
 
    .filter('utcToDate', function(pUTCString) { 
 
    return function(pUTCString) { 
 
     return new Date(pUTCString); 
 
    } 
 
}); 
 

 

 
    
 
    
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<body ng-app="Test" ng-controller="TestController"> 
 
    {{myDate | utcToDate:myDate }} 
 
</body>

+0

添加你所得到的錯誤。 Plunker會在這裏提供你快速的回答。 –

回答

2

你的JS應該是

angular.module('Test', []) 
    .controller('TestController', ['$scope', function ($scope) { 
     $scope.myDate = 1456106575956; 
    }]) 
    .filter('utcToDate', function() { 
    return function(pUTCString) { 
     return new Date(pUTCString); 
    } 
}); 

HTML是好的,但也可以寫成

<body ng-app="Test" ng-controller="TestController"> 
    {{myDate | utcToDate }} 
</body> 

出了什麼問題?

你不需要在定義功能自定義過濾器,你在這裏

.filter('utcToDate', function(pUTCString) { 

更多做了filters從官方文件來指定的參數。

這裏是一個Working Demo

+1

很好,非常感謝。 –

+0

不客氣!如果您對此感到滿意,請選擇該答案作爲答案,以便將其作爲問題關閉。 –