简体   繁体   English

ng-repeat不遍历json对象

[英]ng-repeat not iterating through json object

The controller which contains the JSON object, which is then passed to the view. 包含JSON对象的控制器,然后将其传递到视图。

'use strict';

//app global variable
//this is the controller that handles post requests
//declare services as dependecies $http, $location, custom service apiServiceWeb
app.controller('menteeMentorsCtrl', function($scope, $http, $cookies, $rootScope, $location, loggedInStatus, setCredentials, apiServiceWeb) {


  //get cookie, and then make api call
  var cookieData = setCredentials.getCookie('globals');

  console.log(cookieData);
  console.log("email " + cookieData.auth.username);
  console.log("userType " + cookieData.auth.userType);
  console.log("token " + cookieData.auth.token);

  var ctr_scope = $scope;
  $scope.users = []; //declare an empty array

  $scope.alert = {};
  $scope.showAlert = false;
  $scope.users = [{
    "activeMentors": [{
      "profession": "test",
      "lastName": "test",
      "jobTitle": "test",
      "chatIdentifier": "e46eacf0f7dbd998aba9957b127b51935cbd4fed68533d5708e22c632b60eaa4d65c81fc3cfe3192112c02db1ab4090bc0f18f8a8fdb8cd60f4368643a9a5dbd",
      "title": "",
      "pushToken": "tets",
      "token": {
        "secureToken": "c243fac3ee241f9cb7c442ec893a9c58d85312b890af8719949057e61c3d6a7670455e178a692f27ae0e07da89b4b5d6b0d4267df799249969c70a829cbbab9d"
      },
      "mentees": [],
      "firstName": "test",
      "profileColour": "test",
      "emailAddress": "test",
      "termsAgreed": test,
      "ambition": {
        "ambition": "test",
        "isPrivate": "test"
      },
      "dob": "21/04/1991",
      "busy": test,
      "describingWords": [
        "tese",
        "test",
        "",
        "",
        ""
      ],
      "userType": "mentor",
      "id": {
        "machineIdentifier": 12374093,
        "processIdentifier": 7963,
        "counter": 705715,
        "timestamp": 1439803213
      },
      "requestedMentees": [],
      "profileImageUrl": "test"
    }],
    "requestedMentors": [{
      "profession": "test",
      "lastName": "test",
      "jobTitle": "test",
      "title": "test",
      "mentees": [],
      "firstName": "test",
      "emailAddress": "test",
      "termsAgreed": false,
      "ambition": {
        "ambition": "",
        "isPrivate": "TRUE"
      },
      "dob": "20/06/80",
      "busy": false,
      "describingWords": [
        "Compassionate",
        "Independent",
        "Passionate",
        "Inquisitive",
        "Creative"
      ],
      "userType": "tets",
      "id": {
        "machineIdentifier": 12374093,
        "processIdentifier": 7963,
        "counter": 705580,
        "timestamp": 1439775250
      },
      "requestedMentees": [],
      "profileImageUrl": "test"
    }]
  }];

});

View: 视图:

This is where I use ng-repeat. 这是我使用ng-repeat的地方。

 <div role="main" class="container theme-showcase">
      <div class="" style="margin-top:90px;">
        <div class="col-lg-8">
            <div class="page-header">
                <h2 id="tables">list items</h2>
            </div>
            <div class="bs-component" ng-controller="menteeMentorsCtrl">
                <div class="alert alert-info" ng-hide=true>
                    <p>Sort key: {{sortKey}}</p>
                    <p>Reverse: {{reverse}}</p>
                    <p>Search String : {{search}}</p>
                </div>
                <form class="form-inline">
                    <div class="form-group">
                        <label >Search</label>
                        <input type="text" ng-model="search" class="form-control" placeholder="Search">
                    </div>
                </form>

<div ng-repeat="test in users.requestedMentors">
 <span>Name : {{ test.firstName }}</span>
 <span>Age : {{ test.lastName }}</span>
</div>


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

There are two parent objects in the json object, when I do test in users.requestedMentors , nothing is returned, why is this happening, thanks. json对象中有两个父对象,当我在users.requestedMentors中进行测试时,什么也没有返回,为什么会发生,谢谢。

You should use 你应该用

<div ng-repeat="test in users[0]['requestedMentors']">
 <span>Name : {{ test.firstName }}</span>
 <span>Age : {{ test.lastName }}</span>
</div>

Explaination 讲解

This is because users is itself an array of objects and you are trying to acccess the first element of it. 这是因为users本身就是array of objects并且您尝试访问它的第一个元素。

Update 更新

If there are multiple objects in the users array you should use ng-repeat 如果users数组中有多个objects ,则应使用ng-repeat

Eg: 例如:

<div ng-repeat="user in users">
  <div ng-repeat="test in user['requestedMentors']">
     <span>Name : {{ test.firstName }}</span>
     <span>Age : {{ test.lastName }}</span>
  </div>
</div>

In your case, users is an array object. 在您的情况下,用户是一个数组对象。 Before accessing the each object inside the user array, you need to loop over the users object first. 在访问用户数组内的每个对象之前,您需要先循环遍历用户对象。 you should have something like this. 你应该有这样的东西。

<div ng-repeat="user in users">
<div ng-repeat="test in user.requestedMentors">
  <span>Name : {{ test.firstName }}</span>
  <span>Age : {{ test.lastName }}</span>
</div>

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

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