0

I am trying to implement a custom filter. I am getting a dependency error from angularjs.

Below is my code:

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>

1
  • Add the error you are getting. Plunker would provide u quick answer here. Commented Mar 8, 2016 at 4:37

1 Answer 1

2

Your JS should be

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

HTML is fine, but can also be written as

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

What went wrong?

You're not required to specify a parameter while defining the function for a custom filter as you have done here

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

More on filters from the official documentation.

Here is a Working Demo

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome! Please select it as an answer if you are satisfied with it, so it can be closed as a question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.