简体   繁体   English

带有$ scope变量中的匿名函数的ng-repeat?

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

I am trying to populate my html tables that navigates through nav-menu. 我正在尝试填充通过导航菜单导航的html表。 My tables create with ng-repeat . 我的表使用ng-repeat创建。 Here is my .js file 这是我的.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;
        }
    };
});

And here is the part of my html file: 这是我的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>

When I run my app in a browser and open the console I see that "case 1 was passed" , "case 2 was passed" and "case 3 was passed" are printing out all the time without stop. 当我在浏览器中运行我的应用程序并打开控制台时,我看到"case 1 was passed""case 2 was passed""case 3 was passed"一直在不停地打印。

Why is that happening? 为什么会这样呢? How can I fix that? 我该如何解决?

在您的交换机盒中添加中断

<div ng-repeat="tab in tabArray"> here tabArray is [2,3,4] so for each tab like (2,3,4) <tbody ng-repeat="car in switchCarList(tab)"> will execute one time. <div ng-repeat="tab in tabArray">这里的tabArray为[2,3,4]因此对于每个选项卡,如(2,3,4), <tbody ng-repeat="car in switchCarList(tab)">将执行一次。

when tab=2 $scope.switchCarList(tab) is called so index inside switch become equal to tab that is 2. so switch (index-1) is equal to switch(1) and case 1 will execute. 当调用tab=2 $scope.switchCarList(tab) switch内的索引等于tab等于2。因此switch (index-1)等于switch(1),情况1将执行。

when tab=3 $scope.switchCarList(tab) is called so index inside switch become equal to tab that is 3. so switch (index-1) is equal to switch(2) and case 2 will execute. 当调用tab=3 $scope.switchCarList(tab) switch内的索引等于tab等于3。因此switch (index-1)等于switch(2)并执行情况2。

when tab=4 $scope.switchCarList(tab) is called so index inside switch become equal to tab that is 4. so switch (index-1) is equal to switch(3) and case 1 will execute. tab=4 $scope.switchCarList(tab)调用时,switch内的索引等于tab等于4。因此switch (index-1)等于switch(3),情况1将执行。

thats all. 就这样。 if you dont want to execute all this cases. 如果您不想执行所有这些情况。 then remove <div ng-repeat="tab in tabArray"> 然后删除<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