簡體   English   中英

帶有$ scope變量中的匿名函數的ng-repeat?

[英]ng-repeat with anonymous function that is in a $scope variable?

我正在嘗試填充通過導航菜單導航的html表。 我的表使用ng-repeat創建。 這是我的.js文件

carApp.controller("OtherCarTablesCtrl", function($scope, getAllCars){
    $scope.tabArray = [2,3,4]
    $scope.carList = []

    function sortCars(index){
        getAllCars.get().success(function(data){
            var CarList = [];
            ....
            return CarList;
        });
    }

    $scope.switchCarList = function(index){
        switch (index-1) {
            case 1:
                $scope.carList = sortCars(1);
                console.log("case 1 was passed");
                return $scope.carList;
            case 2:
                $scope.carList = sortCars(2);
                console.log("case 2 was passed");
                return $scope.carList;
            case 3:
                $scope.carList = sortCars(3);
                console.log("case 3 was passed");
                return $scope.carList;
        }
    };
});

這是我的html文件的一部分:

<div ng-controller="OtherCarTablesCtrl">
    <div ng-repeat="tab in tabArray">
        <div class="panel" ng-show="panel.isSelected({{tab}})">
            ....
            <tbody ng-repeat="car in switchCarList(tab)">
            ....
        </div>
    </div>
</div>

當我在瀏覽器中運行我的應用程序並打開控制台時,我看到"case 1 was passed""case 2 was passed""case 3 was passed"一直在不停地打印。

為什么會這樣呢? 我該如何解決?

在您的交換機盒中添加中斷

<div ng-repeat="tab in tabArray">這里的tabArray為[2,3,4]因此對於每個選項卡,如(2,3,4), <tbody ng-repeat="car in switchCarList(tab)">將執行一次。

當調用tab=2 $scope.switchCarList(tab) switch內的索引等於tab等於2。因此switch (index-1)等於switch(1),情況1將執行。

當調用tab=3 $scope.switchCarList(tab) switch內的索引等於tab等於3。因此switch (index-1)等於switch(2)並執行情況2。

tab=4 $scope.switchCarList(tab)調用時,switch內的索引等於tab等於4。因此switch (index-1)等於switch(3),情況1將執行。

就這樣。 如果您不想執行所有這些情況。 然后刪除<div ng-repeat="tab in tabArray">中的<div ng-repeat="tab in tabArray">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM