在循环中使用ng-repeat时更改$ index值

时间:2017-03-21 07:57:23

标签: angularjs indexing angularjs-ng-repeat

使用for循环时我可以执行以下操作



for (var i=0;i<10;i++){
  if(i==2)
  {
    for( i=2;i<=5;i++){
     console.log(i);
    }
  }else{
     console.log(i);
  }
}
&#13;
&#13;
&#13;

我可以通过更改$ index value来使用$ index在ng-repeat中执行相同的操作吗?

3 个答案:

答案 0 :(得分:0)

如果要在controllerjs

中执行上述操作,可以使用angular.foreach
        masterDetailNav.Init("eDocine", "hamburger.png");
        masterDetailNav.Master = FreshPageModelResolver.ResolvePageModel<MenuPageModel>();
        masterDetailNav.Detail = new FreshNavigationContainer(FreshPageModelResolver.ResolvePageModel<HomePageModel>())
        {
            BarBackgroundColor = Settings.IsDoctorUser ? DataHelper.GreenCoor : DataHelper.BlueColor,
            BarTextColor = Color.White
        };

        return masterDetailNav;
    }

    private FreshNavigationContainer CreateLoginNavigation()
    {
        return new FreshNavigationContainer(FreshPageModelResolver.ResolvePageModel<ChooseUserTypePageModel>(), NAVIGATION_LOGIN);
    }
    public MasterDetailPage RootPage
    {
        get
        {
            return MainPage as MasterDetailPage;
        }
    }

如果你想在htm

中这样做
var items=[0,1,2,3]
 angular.forEach(items,function(item,i){
if(i===2 && i<==5)
  {      console.log(i);

  }else{
     console.log(i);
  }
});

答案 1 :(得分:0)

我认为你可以做这样的事情。

&#13;
&#13;
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.arrOne = [0,1,2,3,4,5,6,7,8,9]
$scope.arrTwo = [0,1,2,3,4];
}) 
&#13;
<script data-require="angular.js@1.6.2" data-semver="1.6.2" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.js"></script>
   
<div ng-app="app" ng-controller="ctrl">
 <div ng-repeat="items in arrOne" ng-init="parentIndex = $index"> 
  <div ng-if="parentIndex === 2" ng-repeat="itemch in arrTwo | limitTo: (arrTwo.length - parentIndex) : parentIndex ">   {{itemch}}
  </div>
</div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

&#13;
&#13;
angular.module('app', []).controller('TestController', ['$scope', function($scope) {
    $scope.items = [];
    for(var i = 0; i < 10; i++)
      $scope.items.push({id: i, name: 'name' + i});
}]);
&#13;
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>

<body ng-app="app">
  <div ng-controller="TestController">
 <ul ng-repeat='item in items' ng-init='temp=$index'>
   <li>     
      <!--if(i==2){for(i = 2; i <= 5; i++)}-->
      <span ng-if='temp==2' ng-repeat='item in items | limitTo : (5 - 2 + 1) : 2'>
         {{item | json}}
      </span>
      <!--else{}-->
      <span ng-if='temp!=2'>{{item | json}}</span>      
   </li>   
 </ul>
</div>
</body>
&#13;
&#13;
&#13;