搜尋

首頁  >  問答  >  主體

angular.js - angularjs: ng-repeat 如何實作巢狀?

<p ng-repeat="links in slides">
    <p ng-repeat="link in links">{{link.name}}</p>
</p>

slides是一個二維數組,我以上的程式碼會報錯Error: [ngRepeat:dupes]

$http.get('index.php?option=com_mtree&task=ajax.load').success(function(response) {
    if(response.status) {
        $scope.links = response.links;

        if(typeof response.links != 'undefined') {
            var slides = [], slide;
            for(var i=0; i<response.links.length;) {
                slide = [];
                for(var c=0; c<3&&c<response.links.length; c++, i++) {
                    slide.push(response.links.indexOf(i));
                }
                slides.push(slide);
            }
            $scope.slides = slides;
        }

        /*
        setTimeout(function(){
            jQuery('.saved-list .slideshow').cycle('destroy');
            jQuery('.saved-list .slideshow').cycle();
        }, 0);
        */
    }
});

請問如何實現嵌套?

给我你的怀抱给我你的怀抱2774 天前664

全部回覆(2)我來回復

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-15 16:51:05

    直接的答案:

    <p ng-repeat="links in slides">
        <p ng-repeat="link in links track by $index">{{link.name}}</p>
    </p>
    

    Error: [ngRepeat:dupes]這個出錯提示具體到題主的情況,意思是指數組中有2個以上的相同數字。 ngRepeat不允許collection中存在兩個相同Id的物件

    For example: item in items is equivalent to item in items track by $id(item). This implies that the DOM elements will be associated by item identity in the array.

    對於數字物件來說,它的id就是它本身的值,因此,數組中是不允許存在兩個相同的數字的。為了規避這個錯誤,需要定義自己的track by表達式。例如:item in items track by item.id或者item in items track by fnCustomId(item)。嫌麻烦的话,直接拿循环的索引变量$index来用item in items track by $index

    fiddle範例:http://jsfiddle.net/shiedman/PLV6G/

    回覆
    0
  • 怪我咯

    怪我咯2017-05-15 16:51:05

    http://jsfiddle.net/Nelson_Pan/bcPxe/1

    回覆
    0
  • 取消回覆