我正在向我的控制器中注入$ filter,一切顺利,直到我尝试在promise回调中使用它。这是代码的要点
controller: ['$scope', '$filter', function($scope, $filter) {
geozipcodes.featured().$promise.then(function(res){
$scope.item = $filter('unique')(res, 'name');
});
}]);
抛出'未捕获的ReferenceError:$ filter未定义'
但是,如果我这样做
controller: ['$scope', '$filter', function($scope, $filter) {
var $filter = $filter;
geozipcodes.featured().$promise.then(function(res){
$scope.item = $filter('unique')(res, 'name');
});
}]);
一切都按预期工作,我完全没有。我应该提到这是在if / else语句中,但这不应该有任何影响,我不认为。
有没有人经历过类似的事情?我解决这个问题的方式感觉很糟糕,而且我真的不明白它为什么会起作用。
更新:更多上下文代码
angular.module('ourApp').directive('searchItems', ['$timeout', '$rootScope', 'geozipcodes', 'baselistings', '$compile', '$stateParams',
function($timeout, $rootScope, geozipcodes, baselistings, $compile, $stateParams) {
return {
restrict: "E",
scope: {
performSearch: '&',
emptySetMessage: '@',
itemType: '@',
searchTrigger: '=',
toggleToolbar: '@',
noResults: '=?'
},
templateUrl: 'views/directives/search-items.html',
controller: ['$scope', '$rootScope', 'utils', 'companies', 'settings', 'geozipcodes', '$stateParams', '$filter',
function($scope, $rootScope, utils, companies, settings, geozipcodes, $stateParams, $filter) {
$scope.settings = settings;
$scope.searchType = $scope.itemType == 'student' || $scope.itemType == 'applicant' ? 'student' : $scope.itemType == 'listing' || $scope.itemType == 'application' || $scope.itemType == 'invitation' ? 'listing' : ''
$scope.items = []
$scope.searchInProgress = true;
$scope.initFilters = {};
$scope.pageFilters = {};
$scope.sortList = [];
if ($scope.searchType == 'first') {
// omitted
} else {
if ($scope.itemType == 'listing') {
listingtags.categories().$promise.then(function(res) {
$scope.tagCategories = res;
});
geozipcodes.featured().$promise.then(function(res){
$scope.defaultLocations = $filter('unique')(res, 'name');
});
}
}
...