【问题标题】:Angular JS Filter in JavaScriptJavaScript 中的 Angular JS 过滤器
【发布时间】:2017-01-31 14:32:50
【问题描述】:

我有一个使用 AngularJS 绑定到我的 HTML 的对象数组。单击按钮时,我想过滤数组的内容并仅显示数组中的某些项目。数组中的每个元素都有几个属性,我想过滤“mapid”属性。这是我的代码:

地图视图:

<span ng-class="mapArray[0]" ng-click="MapView(0)" class="active">All Maps</span> | 
<span ng-class="mapArray[1]" ng-click="MapView(1)">Public Art</span> | 
<span ng-class="mapArray[2]" ng-click="MapView(2)">City of Houston</span> | 
<span ng-class="mapArray[3]" ng-click="MapView(3)">Gallery Guide</span></p></div>
var listingsApp = angular.module('listingsApp', []);

listingsApp.controller('pageController', function ($scope, $sce, $filter, $http, $log, $compile) {

$scope.MapView = function (mapviewid) {
    $scope.mapviewid = mapviewid;
    $scope.mapListings = $filter('mapfilter')($scope.allListings, { mapid : mapviewid })
}

})

我收到此代码的 [$injector:unpr] 错误 angular.min.js:117 错误:[$injector:unpr]http://errors.angularjs.org/1.5.7/$injector/unpr?p0=mapfilterFilterProvider%20%3C-%20mapfilterFilter

控制器链接:https://jsfiddle.net/jpking72/7woyjs67/

【问题讨论】:

  • 你在哪里声明了mapfilter,你的控制器的代码/声明是什么?
  • 其余的错误说明了什么?提供者的名字应该是一个大线索
  • 向我们展示您的控制器
  • @Kenny 没有什么可展示的了。我添加了原始声明,并且 $scope.allListings 是正确的。我用 console.log 验证了。

标签: javascript angularjs


【解决方案1】:

可能是你没有定义mapfilter,或者mapfilter是在你没有声明为对你的模块的依赖的模块中定义的,即

 module('myapp', []).filter('mapfilter', function(){ ... })

 module('myapp', ['moduleWithMapFilter'])

【讨论】:

  • 如果您忘记注入某些东西,则不会出现[$injector:unpr] 错误,只有当您注入一些不可用的东西时
  • 这个答案是正确的。您正在尝试使用名称为 mapfilter 的过滤器,但是(至少在您的问题中)不清楚该过滤器是否可用
猜你喜欢
  • 2014-06-14
  • 1970-01-01
  • 2014-09-15
  • 2016-10-18
  • 1970-01-01
  • 2015-01-12
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多