带有angularjs的Json数组不能与ng-repeat指令一起使用

时间:2014-12-11 02:16:18

标签: html json angularjs

我无法在html中显示json中的所有对象,我只获得json数组格式,但是当我更改url时,我可以显示每个对象。我想在html中显示所有json对象。

第一个例子



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

MyApp.controller('MyPerson', function($scope, $http) {
    $http.get("http://127.0.0.1:8000/people/person/?format=json").
        success(function(data) {
            $scope.person = data;
        });
        });



{% verbatim %}
<body>


<div ng-app="MyApp">
<div ng-controller="MyPerson">

<div>


{{person}}

</div>
</div>
</div>

</body>
{% endverbatim %}



{"meta":{"limit":20,"next":null,"offset":0,"previous":null,"total_count":3},"objects":[{"city":"Budapest","id":1,"name":"Igor","resource_uri":"/people/job/person/1/","slug":"person"},{"city":"Warszawa","id":2,"name":"Karol","resource_uri":"/people/job/person/2/","slug":"person1"},{"city":"Jerozolima","id":3,"name":"Michal","resource_uri":"/people/job/person/3/","slug":"Ima"}]}
&#13;
&#13;
&#13;

所以在第一个例子中,我从json获取所有对象,但只有json格式

第二个例子

&#13;
&#13;
var MyApp = angular.module('MyApp', []);

MyApp.controller('MyPerson', function($scope, $http) {
    $http.get("http://127.0.0.1:8000/people/person/1/?format=json").
        success(function(data) {
            $scope.person = data;
        });
        });




{% verbatim %}
<body>


<div ng-app="MyApp">
<div ng-controller="MyPerson">

<div>


{{person.name}}
    {{person.city}}
    {{person.id}}

</div>
</div>
</div>

</body>
{% endverbatim %}



Igor Budapest 1
&#13;
&#13;
&#13;

在第二个例子中,我更改url我输入1是我的对象的id,然后更改我的模板我得到了我想要的html格式的json文件。

第三个例子

&#13;
&#13;
var MyApp = angular.module('MyApp', []);

MyApp.controller('MyPerson', function($scope, $http) {
    $http.get("http://127.0.0.1:8000/people/person/?format=json").
        success(function(data) {
            $scope.persons = data;
        });
        });


{% verbatim %}
<body>


<div ng-app="MyApp">
<div ng-controller="MyPerson">

<div ng-repeat="person in persons">


<p>{{person.name}}</p>
 <p>{{person.city}}</p>
  <p>{{person.id}}</p>

</div>
</div>
</div>

</body>
{% endverbatim %}
&#13;
&#13;
&#13;

在第三个例子中,我应该得到所有对象,但是我的html是清晰且空的。

我尝试了一切,我假设我在模板中做了一切,可能问题是在控制器中,我应该输入不同的URL给jonson?

1 个答案:

答案 0 :(得分:0)

这是因为你的网址&#34; / people / person /?format = json&#34;返回以下对象

{"meta":{"limit":20,"next":null,"offset":0,"previous":null,"total_count":3},
"objects":[{"city":"Budapest","id":1,"name":"Igor","resource_uri":"/people/job/person/1/","slug":"person"},{"city":"Warszawa","id":2,"name":"Karol","resource_uri":"/people/job/person/2/","slug":"person1"},{"city":"Jerozolima","id":3,"name":"Michal","resource_uri":"/people/job/person/3/","slug":"Ima"}]}

此对象只有两个键&#34; meta&#34;和&#34;对象&#34; 。因此,当你在ng-repeat中使用这个对象时,它找不到person.name,person.city和person.id,因为它第一次会找到&#34; meta&#34;对象和下一次&#34;对象&#34;对象都没有名称,城市或身份。

要更正此问题,请更改get方法的成功函数,以将data.objects分配给$ scope.persons

MyApp.controller('MyPerson', function($scope, $http) {
$http.get("http://127.0.0.1:8000/people/person/?format=json").
        success(function(data) {
            $scope.persons = data.objects;
        });
        });