繁体   English   中英

AngularJS带有JSON的多个数组

[英]Angularjs multiple arrays with json

好吧,今天我有一个新问题。 我正在尝试采用此json的多个数组。

[{“ _id”:“ 541200a79fb6706de9164063”,“ index”:0,“ guid”:“ 403d5692-0f07-40e6-84f5-32cdee137127”,“ isActive”:true,“ school”:“ BOILICON”,“ address”: “ 532 Everit Street,威廉姆森,肯塔基州197”,“注册”:“ 2014年3月24日,星期一,凌晨4:46”,“教授”:[{“ 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“}]}

好吧,我尝试在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>$

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

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

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

对此:

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

prof in data.students.professor更改prof in data.students.professor中的prof in students.professor因为您之前已经student in data.students定义了student in data.students因此studentstudent in data.students内部包含professor详细信息

试试看

工作演示

<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>

问题陈述中的几件事,

  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);
}

工作小提琴

暂无
暂无

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

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