Javascript promise回调中没有角度$filter
我正在将$filter注入我的控制器,在我尝试在promise回调中使用它之前,一切都很好。下面是代码的要点Javascript promise回调中没有角度$filter,javascript,angularjs,closures,Javascript,Angularjs,Closures,我正在将$filter注入我的控制器,在我尝试在promise回调中使用它之前,一切都很好。下面是代码的要点 controller: ['$scope', '$filter', function($scope, $filter) { geozipcodes.featured().$promise.then(function(res){ $scope.item = $filter('unique')(res, 'name'); }); }]); 这抛出“未捕获引用错误:$filt
controller: ['$scope', '$filter', function($scope, $filter) {
geozipcodes.featured().$promise.then(function(res){
$scope.item = $filter('unique')(res, 'name');
});
}]);
这抛出“未捕获引用错误:$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');
});
}
}
...
第一个代码段应该可以正常工作,
$filter
可以访问,并且可以在回调中访问。你能复制它吗?是的,每次都是一致的。你能提供更多的代码吗?我对最后一个括号很好奇。它在第一种情况下应该有效。我假设这是一个指令或路由的控制器。您是否在上面某处使用$filter?@Mate为上下文添加了更多代码$筛选器不在上面的任何位置使用它与上下文无关,它与您声明的新promise instanceFirst snippet正常工作的构造函数有关,$filter
可访问将在回调中可访问。你能复制它吗?是的,每次都是一致的。你能提供更多的代码吗?我对最后一个括号很好奇。它在第一种情况下应该有效。我假设这是一个指令或路由的控制器。您是否在上面某处使用$filter?@Mate为上下文添加了更多代码$过滤器不在上面的任何地方使用,它不是关于上下文,而是关于在其中声明新promise实例的构造函数