我是 Angular JS 的新手,正在为出生日期中的月份选项使用自定义过滤器。
monthName 函数已在 Controller 中定义。
当我尝试运行文件时出现错误:-
Uncaught ReferenceError: monthName is not defined
HTML :-
<select ng-model="main.userdobmonth" ng-change="main.getMonthDays()">
<option ng-selected="{{$index+1==main.userdobmonth}}" value="{{$index+1}}"
ng-repeat="a in main.rangeDate(12) track by $index">{{$index+1 | monthName}}</option>
</select>
我的 Controller 代码:-
angular.module('HC', ['ngAnimate'])
.controller('MainController', MainController)
.filter('monthName', monthName);
// Inject dependencies
MainController.$inject = ['U', 'M', 'S', '$filter', '$upload', '$timeout'];
function MainController(U, M, S, $filter, $upload, $scope, $timeout) {
// ....
function monthName() {
return function (monthNumber) { //1 = January
var monthNames = ['January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'];
return monthNames[monthNumber - 1];
}
}
}
有什么解决办法吗?
最佳答案
您在 MainController
中定义了 monthName
。因此,它是一个局部变量,在您尝试使用它的地方之外无法访问。解决方案是将其移到外面。
function MainController (U, M, S, $filter, $upload, $scope, $timeout) { ... }
function monthName () { ... }
关于javascript - Angular js自定义过滤器未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28736649/