简体   繁体   English

使用ng-repeat在angularJS中的对象内部打印带有对象的Json

[英]Print Json with object inside object in angularJS using ng-repeat

Hi guys I have this following JSON external file: 大家好,我有以下JSON外部文件:

{
   "success":true,
   "errors":[

   ],
   "objects":[
      {
         "cod":"8211300",
         "descricao":"Serviços advocatícios"
      },
      {
         "cod":"7111100",
         "descricao":"Serviços de arquitetura"
      },
      {
         "cod":"6204000",
         "descricao":"Consultoria em tecnologia da informação"
      },
      {
         "cod":"6622300",
         "descricao":"Corretores e agentes de seguros, de planos de previdência complementar e de saúde"
      },
      {
         "cod":"8630504",
         "descricao":"Atividade odontológica com recursos para realização de procedimentos cirúrgicos"
      },
      {
         "cod":"7410202",
         "descricao":"Design de interiores"
      },
      {
         "cod":"6202300",
         "descricao":"Desenvolvimento e licenciamento de programas de computador customizáveis"
      },
      {
         "cod":"7112000",
         "descricao":"Serviços de engenharia"
      },
      {
         "cod":"8599699",
         "descricao":"Outras atividades de ensino não especificadas anteriormente"
      },
      {
         "cod":"6391700",
         "descricao":"Agências de notícias"
      },
      {
         "cod":"7311400",
         "descricao":"Agências de publicidade"
      },
      {
         "cod":"8211300",
         "descricao":"Serviços combinados de escritório e apoio administrativo"
      },
      {
         "cod":"9511800 ",
         "descricao":"Reparação e manutenção de computadores e de equipamentos periféricos"
      },
      {
         "cod":"8630503",
         "descricao":"Atividade médica ambulatorial restrita a consultas"
      },
      {
         "cod":"1",
         "descricao":"Outras atividades"
      }
   ]
}

As you can see there's one string inside the object "objects" and I a newbie in angular and facing issues to print this inside my li with ng-repeat. 如您所见,对象“ objects”中有一个字符串,我遇到了一个新手,遇到了一些尖锐的问题,要用ng-repeat在li中打印此字符串。

Here's my app.js 这是我的app.js

var app = angular.module('servicos', []);


app.controller('servicos_1', function($scope, $http){

$http.get("https://app-dot-contabilizei-jobs.appspot.com/rest/simulador/atividades").success(function(data){
    $scope.cliente  = data;
    console.log(data);
}); 

});

And here my index.html 这是我的index.html

<!DOCTYPE html>
<html ng-app="servicos">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS display</title>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.28/angular.js" data-semver="1.2.28"></script>
    <script src="js/app.js"></script>
  </head>

  <body ng-controller="servicos_1">
    <div>
       <span class="city">City</span><span>Rank</span>
    </div>
    <ul class="table" >
       <li class="city-row" ng-repeat="value in cliente">
          <span class="city-name">{{value.cod}}</span>    
          <span class="city-count">{{value.descricao}}</span>
       </li>
    </ul>

  </body>

</html>

I saw a few questions here but none with object inside object. 我在这里看到了几个问题,但没有一个对象在对象内部。 Can you help me guys? 你能帮我吗?

Another question, there are another 2 externals jsons to request via http.get. 另一个问题,还有另外2个外部json可以通过http.get请求。 I can create another controllers but how can I manage this if my body ng-controller is already with my first controller? 我可以创建另一个控制器,但是如果我的身体ng-controller已经在第一个控制器中,该如何管理呢?

Thanks for your time! 谢谢你的时间!

You have to point your $scope.cliente variable to data.object instead of just data like this, 您必须将$scope.cliente变量指向data.object而不是仅像这样的data

<ul class="table" >
    <li class="city-row" ng-repeat="value in cliente.object">
       <span class="city-name">{{value.cod}}</span>    
       <span class="city-count">{{value.descricao}}</span>
     </li>
</ul>

Hope this helps! 希望这可以帮助!

In fact it's in your controller : 实际上它在您的控制器中:

$scope.cliente  = data.data.objects;

Or in your view : 或您认为:

<li class="city-row" ng-repeat="value in cliente.data.objects">

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

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