简体   繁体   English

AngularJS显示复杂对象

[英]AngularJS display complex Object

i have the following JSON: 我有以下JSON:

{
   "_id": "543e95d78a1cec2a38ed53ec",
   "result": {
      "CAR009": [
         {
            "name": "BMW"
         },
         {
            "name": "MERCEDES"
         }
      ],
      "BUS007": [
         {
            "name": "RENAULT"
         }
      ]
   }
}

I don't know in advance "CAR009" and "BUS007" is just for the example. 我事先不知道“ CAR009”和“ BUS007”仅作为示例。

I want it to display in my HTML: 我希望它显示在我的HTML中:

CAR009 : BMW, MERCEDES CAR009:宝马,奔驰

BUS008 : RENAULT BUS008:雷诺

Controller: 控制器:

$http.get('http://localhost:3000/categories/api)
  .success(function (cars) {
       var categoArray = [];
       for (var key in cars.result) {
          var car = cars.result[key];
          for (var idx in car) {
             categoArray.push(car[idx].category_name);
          }
        }

        $scope.categories = categoArray;
    })
   .error(function (cars) {
      console.log(tags);
});

Then in my HTML : (but it is not exactly what I want) 然后在我的HTML中 :(但这并不是我想要的)

<div ng-repeat="categorie in categories">
       <li>{{categorie}}
</div>

I store in an array but I need to store in an Object ? 我存储在数组中,但我需要存储在Object中吗?

How I can do that to make it easier to diplay this with AngularJS? 我如何做到这一点,以使其更容易与AngularJS一起玩呢?

Thanks for your help ! 谢谢你的帮助 !

Assuming that categories has this structure: 假设categories具有以下结构:

    {
      "CAR009": [
         {
            "name": "BMW"
         },
         {
            "name": "MERCEDES"
         }
      ],
      "BUS007": [
         {
            "name": "RENAULT"
         }
      ]
   }

You want to do it like this: 您想要这样做:

<ul>
   <li ng-repeat="(key, cars) in categories">
       {{key}}: <span ng-repeat="car in cars">{{car.name}}{{!$last?', ':''}}</span>
   </li>
</ul>

Example

Also, you may want to change this part of your code: 另外,您可能需要更改代码的这一部分:

$http.get('http://localhost:3000/categories/api')
  .success(function (cars) {
        $scope.categories = cars.result;
    })
   .error(function (cars) {
      console.log(tags);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM