我的自定义过滤器给了我一个错误。
未捕获的TypeError:无法读取未定义的属性“过滤器”
代码:
angular.module('PatientApp', []).filter('ageFilter', function() {
// birthday is a date
function calculateAge(birthday) {
var dateOut = new Date(birthday);
dateOut.setDate(dateOut.getDate() + 1);
var ageDifMs = Date.now() - dateOut .getTime();
// miliseconds from epoch
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
return function(birthdate) {
return calculateAge(birthdate);
};
});
答案 0 :(得分:0)
这对我来说似乎很好。没有更多细节,很难重现错误。但是您的过滤器代码似乎是正确的。
angular.module('PatientApp', []).filter('ageFilter', function() {
// birthday is a date
function calculateAge(birthday) {
var dateOut = new Date(birthday);
dateOut.setDate(dateOut.getDate() + 1);
var ageDifMs = Date.now() - dateOut .getTime();
// miliseconds from epoch
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
return function(birthdate) {
return calculateAge(birthdate);
};
})
.controller('mainCtrl', function($scope, $filter){
var ageFilter = $filter('ageFilter');
$scope.age = ageFilter('12/25/1995');
});