Lock
Lock

Reputation: 5522

Angular filter throwing "Can't interpolate" error even though the filter works

I have the below filter (why I got from StackOverflow) which works on one page, but throws the below error on another (using the same object).

app.filter('dateFormat', function dateFormat($filter){
  return function(text){
    var  tempdate= new Date(text.replace(/-/g,"/"));
    return $filter('date')(tempdate, "dd-MM-yyyy");
  }
});

Can't interpolate: {{ job.job_date | dateFormat}}
TypeError: text is undefinedO/<@http://localhost/ef-serial-numbers/public/assets/global/plugins/angularjs/angular.min.js:6:412Ka.interr@http://localhost/ef-serial-numbers/public/assets/global/plugins/angularjs

If I change job.job_date to a hardcoded date, the error disappears. The thing is, the filter is actually working correctly regardless of the error.

Upvotes: 0

Views: 451

Answers (1)

MMhunter
MMhunter

Reputation: 1441

Maybe check if text is undefined before calling .replace.

Since at the very beginning, the filter function may be called before job.job_date gets its value.

Upvotes: 2

Related Questions