猿问

嵌套ng-repeat

我有一些虚拟XML文件:


<Week number="2013-W45">

    <Day dow="1" templateDay="Monday">

        <Job name="wake up" >

            <Job name="get dressed" >

                <Job name="prepare breakfast" >

                    <Job name="eat breakfast" > </Job>

                </Job>

            </Job>

        </Job>

        <Job name="work 9-5" >

        </Job>

    </Day>

    <Day dow="2" templateDay="Tuesday"   >

        <Job name="wake up" >

            <Job name="get dressed" >

                <Job name="prepare breakfast" >

                    <Job name="eat breakfast" > </Job>

                </Job>

            </Job>

        </Job>

        <Job name="work 9-5" >

        </Job>

        <Job name="football" >

        </Job>

    </Day>

    <Day dow="3"   templateDay="Wednesday" >

        <Job name="wake up" >

            <Job name="get dressed" >

                <Job name="prepare breakfast" >

                    <Job name="eat breakfast" > </Job>

                </Job>

            </Job>

        </Job>

        <Job name="work 9-5" >

        </Job>

    </Day>

    <Day dow="4"  templateDay="Thursday"  >

        <Job name="wake up" >

            <Job name="get dressed" >

                <Job name="prepare breakfast" >

                    <Job name="eat breakfast" > </Job>

                </Job>

            </Job>

        </Job>

        <Job name="work 9-5" >

        </Job>

        <Job name="football" >

        </Job>

    </Day>

    <Day dow="5" templateDay="Friday" >

        <Job name="go to pub" >

        </Job>

    </Day>

   

使用此库http://code.google.com/p/x2js/将其转换为json

Day可以有任意数量的Job,Job可以嵌套并且包含任意数量的其他Job。


现在使用此代码


<p ng-repeat="day in myData.Week.Day">

   {{day._dow}} - {{day._templateDay}}

</p>

我可以列出有效的日期。我希望使用以下代码


<p ng-repeat="day in myData.Week.Day">

    {{day._dow}} - {{day._templateDay}}


    <span ng-repeat="job in day.Job">

        {{job._name}}

    <span/>

</p>

我可以列出当天的工作日和最高职位,但这是行不通的。(要列出嵌套的Jobs,将是下一个任务,而不是现在询问该任务)。


那么,如何至少列出顶级职位?同样在json格式中,我看到一些Jobs Objects和一些are Arrays。如何处理两种情况?


PS .:使用角度1.2.0-rc.3


一只名叫tom的猫
浏览 594回答 3
3回答

翻过高山走不出你

如果您有一个很大的嵌套JSON对象,并在多个屏幕上使用它,则可能会在页面加载中遇到性能问题。我总是去寻找小的单个JSON对象,并仅在需要它们时才将相关对象作为延迟加载查询。您可以使用ng-init来实现<td class="lectureClass" ng-repeat="s in sessions" ng-init='presenters=getPresenters(s.id)'>&nbsp; &nbsp; &nbsp; {{s.name}}&nbsp; &nbsp; &nbsp; <div class="presenterClass" ng-repeat="p in presenters">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {{p.name}}&nbsp; &nbsp; &nbsp; </div></td>&nbsp;控制器端的代码应如下所示$scope.getPresenters = function(id) {&nbsp; &nbsp; return SessionPresenters.get({id: id});};而API工厂如下:angular.module('tryme3App').factory('SessionPresenters', function ($resource, DateUtils) {&nbsp; &nbsp; &nbsp; &nbsp; return $resource('api/session.Presenters/:id', {}, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'query': { method: 'GET', isArray: true},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'get': {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: 'GET', isArray: true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'update': { method:'PUT' }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });

倚天杖

创建一个不会在页面上呈现的虚拟标签,但它将用作ng-repeat的持有人:<dummyTag ng-repeat="featureItem in item.features">{{featureItem.feature}}</br> </dummyTag>
随时随地看视频慕课网APP

相关分类

AngularJS
我要回答