Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 基于类别获取json对象_Angularjs_Json - Fatal编程技术网

Angularjs 基于类别获取json对象

Angularjs 基于类别获取json对象,angularjs,json,Angularjs,Json,我有一个像这样的json [ { "id":1, "name": "BMW E46 PANDEM", "price": 130000, "curency":"USD", "color":"white", "category":"car" }, { "id":2, "name": "Yamaha", "price": 2000, "curency":"USD", "color":"white",

我有一个像这样的json

[
  { 
    "id":1,
    "name": "BMW E46 PANDEM",
    "price": 130000,
    "curency":"USD",
    "color":"white",
    "category":"car"
  },
  { 
    "id":2,
    "name": "Yamaha",
    "price": 2000,
    "curency":"USD",
    "color":"white",
    "category":"Bike",
  },
  { 
    "id":3,
    "name": "Nissan GTR R34",
    "price": 130000,
    "curency":"USD",
    "color":"black",
    "category":"car"
  }
]
angular
    .module('app')
    .factory('ItemFactory', ['$http', function($http)
        {
            return {
               get: function() {
                return $http.get('scripts/item.json').then(function(response) {
                    return response.data;
                });
               }
            };
        }
    ]);
像这样的工厂

[
  { 
    "id":1,
    "name": "BMW E46 PANDEM",
    "price": 130000,
    "curency":"USD",
    "color":"white",
    "category":"car"
  },
  { 
    "id":2,
    "name": "Yamaha",
    "price": 2000,
    "curency":"USD",
    "color":"white",
    "category":"Bike",
  },
  { 
    "id":3,
    "name": "Nissan GTR R34",
    "price": 130000,
    "curency":"USD",
    "color":"black",
    "category":"car"
  }
]
angular
    .module('app')
    .factory('ItemFactory', ['$http', function($http)
        {
            return {
               get: function() {
                return $http.get('scripts/item.json').then(function(response) {
                    return response.data;
                });
               }
            };
        }
    ]);
控制器

angular
    .module('app')
    .controller('apparelCtrl',['$scope','ItemFactory', function($scope,ItemFactory){
            $scope.title='car';
            ItemFactory.get().then(function(data) {
                $scope.apparelItems = data;
            });
            $scope.apparelItems=ItemFactory.get();
            console.log($scope.apparelItems);
        }]);
指令.js

angular
    .module('app.directives.gridViewApparel',[])
    .directive('gridApparel',function()
    {
        return{
            restrict:'E',
            scope:{
                data: '='
            },
            transclude:true,
            replace:true,
            templateUrl:"templates/directives/gridViewApparel.html",
            controller:function($scope){
                console.log($scope.data);
            }
        };
    }
);
diirective.html

<div>
    <div class="col-sm-4 ApparelThumbnail">
        <p class="title">{{data.name}}</p>
        <p>{{data.price}}{{curency}}</p>
        <a href="#/Apparel/{{data.id}}" class="btn btn-primary">detail</a>
    </div>
</div>
我尝试在筛选器之前添加“按索引跟踪”,但仍然得到相同的错误。 如何根据类别从json中获取对象?
谢谢

以下代码没有任何意义

ItemFactory.get().then(function(data) {
    $scope.apparelItems = data;
});
$scope.apparelItems=ItemFactory.get();

删除
$scope.apparelItems=ItemFactory.get(),因为您正在重写
$scope.apparelItems=data$scope.apparelItems
等于一个承诺,而不是该承诺的结果。

您的返回是个问题。分析你的回报是什么,代表、数据还是承诺。理解$http响应

谢谢你的帮助

他的代码解决了这个问题


他建议我删除
$scope.apparelItems=ItemFactory.get()
现在它的修复程序

错误可能是由
网格视图服装
引起的。也许你可以对你正在使用的指令
grid view apparel
做一些额外的解释。@pengy是的,先生,我将更新我的代码,同时发布
gridViewApparel.html
$scope.apparelItems=ItemFactory.get()的代码@Phil哦,这是固定的。谢谢你,先生。那密码怎么了。我想我需要捕捉工厂数据