如何在角度js中显示来自另一个表嵌套ng-repeat的数据

时间:2016-08-15 06:54:42

标签: javascript php angularjs

我可以使用ng-repeat标签显示一个表中的数据。但问题是当我尝试根据之前的ng-repeat显示来自另一个表的数据时。

详细说明我有2个表格用于部分

section_id  section_name

1              php

2              java

另一张桌子是讲座

id  section_id lecture_name
1     2          basics
2     1          loops

我可以使用ng-repeat显示部分。但我不能在相应的部分下面展示讲座

       <div class="course_curriculum " ng-repeat="section in sections">

                     <ul class="sections" >
                                <h4>Section {{$index+1}}: {{section.section_name}}</h4>
                                <!--<h4>Section 1: Introduction to Mobile Application Development</h4>-->
                                <li>
                                    <div class="section-head" ng-click="view_lecture(section.id)" data-toggle="collapse" data-target="#section{{$index+1}}" >
                                        <i class="fa fa-bars" aria-hidden="true"></i> Course Lectures
                                    </div>

                                    <ul id="section{{$index+1}}" class="collapse" >
                                        <li>
                                            <ul ng-repeat="lecture in lectures">
                                                <li >
                                                    <div class="section-sub-head" data-toggle="collapse" data-target="#sub1">
                                                        <i class="fa fa-bars" aria-hidden="true"></i> {{lecture.lecture_name}}
                                                    </div>
                                                    <ul id="sub1" class="collapse lecture">

                                                        <li>

                                                            <div class="col-md-5 article-show" >
                                                                <img src="html/img/file.png" style="width: 100px; background-color: black ;margin-right: 10px" />

                                                                Article<br>
                                                                <a  data-toggle="modal" data-target="#text"><i class="fa fa-pencil" aria-hidden="true"></i> &nbsp;Edit Content</a> <br>
                                                                <a href="#" ><i class="fa fa-play" aria-hidden="true"></i>&nbsp;Replace With Video</a> <br>

                                                            </div>
                                                            <div class="col-md-5 right text-right" >

                                                                <div class="btn-group" role="group">
                                                                    <button type="button" class="publish">Publish</button>
                                                                </div>
                                                                <div class="btn-group" role="group">
                                                                    <button type="button" class="Preview dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                                                        Preview
                                                                        <span class="caret"></span>
                                                                    </button>
                                                                    <ul class="dropdown-menu">
                                                                        <li><a href="resources">As Instructor</a></li>
                                                                        <li><a href="#">As Student</a></li>
                                                                    </ul>
                                                                </div>
                                                            </div>
                                                            <div class="clearfix"></div>
                                                            <div class="col-md-12">
                                                                <button type="button"   data-toggle="collapse" data-target="#Resources1" class="publish">Add Resources</button>
                                                                <button type="button" class="publish">Add Captions</button>
                                                            </div>
                                                            <div  id="Resources1" class="collapse lecture">
                                                                <ul class="resouse">
                                                                    <li class="active"><a data-toggle="tab" href="#Downloadable-File">Downloadable File</a></li>
                                                                    <li><a data-toggle="tab" href="#from-Library">Add from Library</a></li>
                                                                    <li><a data-toggle="tab" href="#External-Resource">External Resource</a></li>
                                                                    <li><a data-toggle="tab" href="#Source-Code">Source Code</a></li>
                                                                </ul>

                                                                <div class="tab-content">
                                                                    <div id="Downloadable-File" class="tab-pane fade in active">
                                                                        <br/>

                                                                        <div class="col-md-8">
                                                                            <input type="text" class="form-control" placeholder="Add files no larger than 1.0 GiB." disabled>

                                                                        </div>
                                                                        <div class="col-md-4">
                                                                            <label class="upload">
                                                                                upload file<input type="file" style="display: none;">
                                                                            </label>
                                                                        </div>
                                                                        <p>Tip: A resource is for any type of document that can be used to help students in the lecture.
                                                                            This file is going to be seen as a lecture extra. Make sure everything is legible! </p>
                                                                    </div>
                                                                    <div id="from-Library" class="tab-pane fade">
                                                                        <br/>
                                                                        <p>Library is empty. Tip: You can use Accounts.academy File Uploader to upload several files at the same time. </p>
                                                                    </div>
                                                                    <div id="External-Resource" class="tab-pane fade">
                                                                        <br/>
                                                                        <div class="col-md-12">
                                                                            <div class="form-group">
                                                                                <label for="usr">Title:</label>
                                                                                <input type="text" class="form-control" placeholder="A descriptive title" id="usr">
                                                                            </div>
                                                                            <div class="form-group">
                                                                                <label for="pwd">URL:</label>
                                                                                <input type="password" class="form-control" placeholder="https://example.com" id="pwd">
                                                                            </div>
                                                                            <button type="submit" value="Submit" class="publish">Submit</button>
                                                                        </div>
                                                                    </div>
                                                                    <div id="Source-Code" class="tab-pane fade">
                                                                        <br/>

                                                                        <div class="col-md-8">
                                                                            <input type="text" class="form-control" placeholder="Add files no larger than 1.0 GiB." disabled>

                                                                        </div>
                                                                        <div class="col-md-4">
                                                                            <label class="upload">
                                                                                upload File<input type="file" style="display: none;">
                                                                            </label>
                                                                        </div>
                                                                        <p><B>Tip</B>: Only available for Python and Ruby for now. You can upload .py and .rb files</p>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <div class="clearfix"></div>

                                                        </li>
                                                        <li>
                                                            <button type="button"  data-toggle="modal" data-target="#saudio"  class="sub-sub-section-add">Add Audio</button>
                                                            <button type="button" data-toggle="modal" data-target="#svideo"  class="sub-sub-section-add">Add Video</button>
                                                            <button type="button" data-toggle="modal" data-target="#stext"  class="sub-sub-section-add">Add Text/Article</button>
                                                            <button type="button" data-toggle="modal" data-target="#stext"  class="sub-sub-section-add">Add File</button>

                                                        </li>
                                                    </ul>
                                                </li>
                                            </ul>
                                        </li>





                                        <li  >
                                            <div class="section-sub-head">
                                                <form ng-submit="lecture_insert($index+1,section.id)">
                                                <input type="text" class="form-control" id=lecture_name{{$index+1}} ng-model="section.lecture_name" placeholder="Enter a Title" required>
                                                <!--<input type="text" ng-hide  class="form-control" id=section_id{{$index+1}} ng-model="section.id" >-->

                                                <div class="btn-group sub-section-add">

                                                    <button  type="submit"   style="background: rgba(0,0,0,0); border: none;">Add Lecture</button>

                                                </div>
                                                </form>



                                            </div>
                                        </li>
                                    </ul>
                                </li>
                                <li>
                                    <div class="section-head" data-toggle="collapse" data-target="#faq{{$index+1}}" >
                                        <i class="fa fa-bars" aria-hidden="true"></i>  FAQ
                                    </div>
                                    <ul id="faq{{$index+1}}" class="collapse">

                                        <li>
                                            <button type="button" data-toggle="modal" data-target="#mainfaq"  class="section-add">Add More Faq</button>
                                        </li>
                                        <li>
                                            <strong>What is the target audience? </strong><br/>
                                            <P>This course is for those how want to learn how to design a logo,This course is for those how want to learn how to design a logo</P>
                                            <button type="button" class="section-edit">Edit</button>
                                        </li>
                                        <li>
                                            <strong>What is the target audience? </strong><br/>
                                            <P>This course is for those how want to learn how to design a logo,This course is for those how want to learn how to design a logo</P>
                                            <button type="button" class="section-edit">Edit</button>
                                        </li>
                                        <li>
                                            <strong>What is the target audience? </strong><br/>
                                            <P>This course is for those how want to learn how to design a logo,This course is for those how want to learn how to design a logo</P>
                                            <button type="button" class="section-edit">Edit</button>
                                        </li>
                                    </ul>

                                </li>
                                <li>
                                    <div class="section-head" data-toggle="collapse" data-target="#Exam{{$index+1}}" >
                                        <i class="fa fa-bars" aria-hidden="true"></i>  Exam
                                    </div>
                                    <ul id="Exam{{$index+1}}" class="collapse">+
                                        <li >
                                            <div class="section-sub-head">
                                                fgfgh
                                            </div>
                                            <ul >
                                                <li>
                                                    df
                                                </li>
                                                <li>
                                                    df
                                                </li>
                                            </ul>
                                        </li>
                                    </ul>
                                </li>
                                <li>
                                    <!--<div class="btn-group section-add">-->
                                        <!--<button  data-toggle="modal" data-target="#lecture"  type="button" style="background: rgba(0,0,0,0); border: none;">Add Lecture</button>-->

                                    <!--</div>-->

                                    <!--<div class="btn-group section-add">-->
                                        <!--<button  data-toggle="modal" data-target="#exam"  type="button" style="background: rgba(0,0,0,0); border: none;">Add Exam</button>-->

                                    <!--</div>-->
                                    <!--<div class="btn-group section-add">-->
                                        <!--<button  data-toggle="modal" data-target="#mainfaq"  type="button" style="background: rgba(0,0,0,0); border: none;">Add FAQ</button>-->

                                    <!--</div>-->

                                </li>
                            </ul>
                        </div>

2 个答案:

答案 0 :(得分:1)

由于您希望根据上述section_id有条件地展示讲座,因此您可以使用ng-if来获得所需的结果。 E.g。

<div class="section" ng-repeat="sec in sections">
    <span>Section Id: <i>{{sec.section_id}}</i></span>
    <span>Section Name: <i>{{sec.section_name}}</i></span>
    <div class="lecture" ng-repeat="lec in lectures" ng-if="lec.section_id==sec.section_id"> <!--nested ng-repeat for lectures-->
      <span>Lecture Id: <i>{{lec.lecture_id}}</i></span>
      <span>Lecture Name: {{lec.lecture_name}}</span>
    </div>
  </div>

在JS中我的数据采用以下格式:

$scope.sections=[
    {section_id:1,section_name:"php"},
    {section_id:2,section_name:"java"}
  ];
  $scope.lectures = [
    {section_id:1,lecture_id:2,lecture_name:"basics"},
    {section_id:2,lecture_id:1,lecture_name:"loops"}
  ];

我已准备好JSBIN,以便您了解正在发生的事情。

答案 1 :(得分:1)

提供的代码中有几个 可能的 问题:

  1. 您的内心ng-repeat位于错误的元素上。它应位于其正上方的<li>元素上。在bootstrap中嵌套列表时,你永远不会重复<ul>而不将每个列表包裹在至少<li>(通常也是<a>,其中包含下拉文本)。
  2. 要实现嵌套表(每个部分中的讲座列表),您希望lectures变量位于每个section上。这会使您的内部 ng-repeat类似于="lecture in section.lectures"
  3. Here is a working JSBin我在上面列出了我所做的更改。我省略了很多HTML,因为它在没有其他控制器的情况下被破坏了,但我留下了足够的内容来显示如何使嵌套重复工作。