【问题标题】:Angularjs multiple arrays with json带有json的Angularjs多个数组
【发布时间】:2014-09-12 05:34:06
【问题描述】:

今天我有一个新问题。我正在尝试获取此 json 的多个数组。

[ { "_id" : "541200a79fb6706de9164063" , "index" : 0 , "guid" : “403d5692-0f07-40e6-84f5-32cdee137127”,“isActive”:真,“学校” : "BOILICON" , "地址" : "532 Everit Street, Williamson, Kentucky, 197”,“注册”:“2014 年 3 月 24 日,星期一 4:46 AM”,“教授”: [ { "id" : 0 , "name" : { "first" : "Mclean" , "last" : "Robertson"} , “电子邮件”:“mclean.robertson@undefined.com”,“年龄”:36,“电话”: "+1 (946) 436-2567" , "coursePrice" : "$4,882.28" , "favoriteParadigm" : “功能”} ]}

好吧,我尝试在 html 代码上这样做:

$<div ng-controller="studentsController">
    <table>    
        <tr ng-repeat= "student in data.students">
            <td>{{student.index}}</td>
            <td>{{student.guid}}</td>
            <td>{{student.isActive}}</td>
            <td>{{student.school}}</td>
            <td>{{student.address}}</td>
            <td ng-repeat="prof in data.students.professor"> 
                email: {{prof.email}}
            </td>
        </tr>
    </table>
</div>$

但这对我不起作用。谁能帮帮我?

【问题讨论】:

  • 这里的json不正确。是这个原因吗?
  • json 无效,你错过了最后一个方括号
  • 嗯,我不确定这是 json 链接 link
  • 这不是我剪切 json 的问题,因为 json 太大了,有链接jsonPage

标签: javascript json html angularjs


【解决方案1】:

prof in data.students.professor 更改为prof in students.professor,因为之前您已定义student in data.students,因此student 将在内部包含professor 详细信息

试试这个

Working Demo

<div ng-app='myApp' ng-controller="studentsController">
    <table border="1">
        <tr ng-repeat="student in students">
            <td>{{student.index}}</td>
            <td>{{student.guid}}</td>
            <td>{{student.isActive}}</td>
            <td>{{student.school}}</td>
            <td>{{student.address}}</td>
            <td ng-repeat="prof in student.professor">email: {{prof.email}}</td>
        </tr>
    </table>
</div>

【讨论】:

    【解决方案2】:

    在 ng-repeat 中,您有不同的范围,并且当前项目可以直接访问。所以改变

    <td ng-repeat="prof in data.students.professor">
    

    到这里:

    <td ng-repeat="prof in student.professor"> 
        email: {{prof.email}}
    </td>
    

    【讨论】:

      【解决方案3】:

      问题陈述中的几件事,

      1. 首先,您需要避免重复,因此在您的 ng-repeat 中按表达式进行跟踪。
      2. 由于您将 json 存储在字符串中,因此您需要使用 JSON.parse 将其解析为 JSON。
      3. 就像@Zaheer 提到的那样,因为您需要引用外部 ng-repeat,所以您需要引用学生而不是 data.students.professor 中的教授

      所以您的代码将如下所示:

      HTML:

      <div ng-app>
          <table ng-controller="test">
              <tr ng-repeat="student in data.students track by $index">
                  <td>{{student.index}}</td>
                  <td>{{student.guid}}</td>
                  <td>{{student.isActive}}</td>
                  <td>{{student.school}}</td>
                  <td>{{student.address}}</td>
                  <td ng-repeat="prof in student.professor track by $index">email: {{prof.email}}</td>
              </tr>
          </table>
      </div>
      

      JS:

      function test($scope) {
          $scope.data = {};
          var students = '[ { "_id" : "541200a79fb6706de9164063" , "index" : 0 , "guid" : "403d5692-0f07-40e6-84f5-32cdee137127" , "isActive" : true , "school" : "BOILICON" , "address" : "532 Everit Street, Williamson, Kentucky, 197" , "registered" : "Monday, March 24, 2014 4:46 AM" , "professor" : [ { "id" : 0 , "name" : { "first" : "Mclean" , "last" : "Robertson"} , "email" : "mclean.robertson@undefined.com" , "age" : 36 , "phone" : "+1 (946) 436-2567" , "coursePrice" : "$4,882.28" , "favoriteParadigm" : "funcional"} ]}]';
          $scope.data.students = JSON.parse(students);
      }
      

      Working Fiddle

      【讨论】:

        猜你喜欢
        • 2016-08-25
        • 1970-01-01
        • 2018-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多