Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.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
使用angular进行json迭代时遇到问题_Json_Angularjs_Loops - Fatal编程技术网

使用angular进行json迭代时遇到问题

使用angular进行json迭代时遇到问题,json,angularjs,loops,Json,Angularjs,Loops,我尝试使用angulajs迭代以下json "[{ \"product_id\":\"1517519077\", \"prod_name\":\"Desktop PC Dell Precision T5610 Workstation\", \"gpu\":\"QuadroK4000\", \"cpu\":\"\", \"ram_cap\":\"8\", \"ram_type\":\"DDR3\", \"hdd_cap\":\"500\", \"hdd_speed\":\"7200\", \"hd

我尝试使用angulajs迭代以下json

"[{
\"product_id\":\"1517519077\",
\"prod_name\":\"Desktop PC Dell Precision T5610 Workstation\",
\"gpu\":\"QuadroK4000\",
\"cpu\":\"\",
\"ram_cap\":\"8\",
\"ram_type\":\"DDR3\",
\"hdd_cap\":\"500\",
\"hdd_speed\":\"7200\",
\"hdd_type\":\"HDD\",
\"display_size\":\"\",
\"display_res\":\"\",
\"price\":\"2319\",
\"cpu_rank\":\"0\",
\"cpu_rate\":\"0\",
\"gpu_rank\":\"2853\",
\"gpu_rate\":\"38\",
\"category\":\"2\",
\"tld\":\"de\",
\"feed_id\":\"1\",
\"gewicht\":null,
\"os\":null,
\"brand\":\"DELL\",
\"update_time\":null,
\"laufwerk\":null},

{\"product_id\":\"1534720291\",
\"prod_name\":\"Desktop PC Dell Precision T3610 BTX Base\",
\"gpu\":null,
\"cpu\":\"\",
\"ram_cap\":\"8\",
\"ram_type\":\"DDR3\",
\"hdd_cap\":\"500\",
\"hdd_speed\":\"7200\",
\"hdd_type\":\"HDD\",
\"display_size\":\"\",
\"display_res\":\"\",
\"price\":\"1164\",
\"cpu_rank\":\"0\",
\"cpu_rate\":\"0\",
\"gpu_rank\":null,
\"gpu_rate\":null,
\"category\":\"2\",
\"tld\":\"de\",
\"feed_id\":\"1\",
\"gewicht\":null,
\"os\":null,
\"brand\":\"DELL\",
\"update_time\":null,
\"laufwerk\":null
}]"
但我得到
错误:[ngRepeat:dupes]中继器中不允许重复。使用“跟踪依据”表达式指定唯一关键点。中继器:产品中的产品,重复键:字符串:“
错误

hier是我如何获取和迭代数据的方法

控制器

function ProductsCtrl($scope, $http ) {
    $http({
        method: 'GET',
        url: 'http://www.someapi.com/api/restapi/products.json'
   }).success(function(data, status, headers, config) {
        // this callback will be called asynchronously       
        $scope.products = data;
        console.log($scope.products);
    }).error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        console.log(headers);
        console.log(data);
    });
}
查看迭代

<ul ng-repeat="product in products">
       <li>{{ product.prod_name }}</li>
</ul>
  • {{product.prod_name}
试用产品中的
产品按$index跟踪
我遇到浏览器崩溃

A)如果您得到“不允许在中继器中重复”,您需要将
按$index跟踪
添加到ngRepeat

示例:

<div ng-repeat="product in products track by $index">
var data="[{\"product_id\":\"1517519077\",\"prod_name\":\"Desktop PC Dell Precision T5610 Workstation\",\"gpu\":\"QuadroK4000\",\"cpu\":\"\",\"ram_cap\":\"8\",\"ram_type\":\"DDR3\",\"hdd_cap\":\"500\",\"hdd_speed\":\"7200\",\"hdd_type\":\"HDD\",\"display_size\":\"\",\"display_res\":\"\",\"price\":\"2319\",\"cpu_rank\":\"0\",\"cpu_rate\":\"0\",\"gpu_rank\":\"2853\",\"gpu_rate\":\"38\",\"category\":\"2\",\"tld\":\"de\",\"feed_id\":\"1\",\"gewicht\":null,\"os\":null,\"brand\":\"DELL\",\"update_time\":null,\"laufwerk\":null},{\"product_id\":\"1534720291\",\"prod_name\":\"Desktop PC Dell Precision T3610 BTX Base\",\"gpu\":null,\"cpu\":\"\",\"ram_cap\":\"8\",\"ram_type\":\"DDR3\",\"hdd_cap\":\"500\",\"hdd_speed\":\"7200\",\"hdd_type\":\"HDD\",\"display_size\":\"\",\"display_res\":\"\",\"price\":\"1164\",\"cpu_rank\":\"0\",\"cpu_rate\":\"0\",\"gpu_rank\":null,\"gpu_rate\":null,\"category\":\"2\",\"tld\":\"de\",\"feed_id\":\"1\",\"gewicht\":null,\"os\":null,\"brand\":\"DELL\",\"update_time\":null,\"laufwerk\":null}]"

  $scope.products=angular.fromJson(data);

实时示例:

在这种情况下,浏览器崩溃了您是说斜杠产生了错误?我从restapi获取数据作为json转换。您需要使用angular.fromJson(数据);我更新了我的回答您使用angular.fromJson(您的json字符串)了吗?是的,看一下回复看起来不错,但列表不应该太长,我只有10个项目。您可以试试