带有过滤器的AngularJS ng-repeat-notarray error,期望的数组,但收到:

时间:2018-11-04 14:41:35

标签: angularjs

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;
        });
}]);

template-details.html

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

1 个答案:

答案 0 :(得分:1)

如错误所示,过滤器需要获取数组作为输入,但是要传递对象,请尝试 response.data

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