2016-03-08 85 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

不客气!如果您对此感到满意,请选择该答案作为答案,以便将其作为问题关闭。 –