首页 > 解决方案 > AngularJS ng-repeat 带有过滤器 - notarray 错误,预期的数组但收到:

问题描述

templates.js 这是控制器文件

.controller('TemplatesDetailsCtrl', ['$scope', '$routeParams', '$http', '$filter', function($scope, $routeParams, $http, $filter){
    var templateId = $routeParams.templateId;
    $http.get('json/templates.json')
        .then(function(response){
            $scope.templates = $filter('filter')(response , function(d){
                return d.id == templateId
            })[0];
            $scope.mainImage = $scope.template.images[0].name;
        });
}]);

模板详细信息.html

<img class="img-full" src="img/{{mainImage}}">

标签: angularjs

解决方案


正如错误所说,过滤器需要将数组作为输入,但是您正在传递一个对象,请尝试response.data

 $scope.templates = $filter('filter')(response.data , function(d){
                return d.id == templateId
 })[0];

推荐阅读