简体   繁体   English

for循环内的AngularJS范围

[英]AngularJS scope inside a for loop

var coutas = [];

  for (var i = 0; i < d.length; i++) {
    coutas.push(JSON.parse(d[i].coutas));
  }

 for (var i = 0; i < coutas.length; i++) {
    $scope.coutas = coutas[i];
 }

That doesn't work 那不行

what I try to do is assign the variable coutas the scope.coutas 我想做的是为变量coutas分配scope.coutas

i'm using two for loop because that : 我正在使用两个for循环,因为:

在此处输入图片说明

the firt loop , It is to tour the first array, and the second loop is to tour the seconds array firt循环,它是浏览第一个数组,第二个循环是浏览秒数组

example : the firts loop tour the array 0 but inside the array 9 they are 22 objets. 示例:firts循环浏览数组0,但在数组9内它们是22个对象。

i want that : 我要那个 : 在此处输入图片说明

Instead of using two for loop you may solve this problem using single loop. 您可以使用单循环解决此问题,而不是使用两个for循环。 In your solution you generate coutas array in first loop then in second loop assigned only last info of var coutas in angular variable not whole coutas . 在您的解决方案中,您将在第一个循环中生成coutas数组,然后在第二个循环中仅在角度变量中分配var coutas最后一个信息,而不是整个coutas

One process can be assign whole coutas after first loop. 在第一个循环之后,可以为一个过程分配整个coutas

like: 喜欢:

$scope.coutas = coutas

or you can push in $scope.coutas in first loop. 或者,您可以在第一个循环中放入$scope.coutas

like: 喜欢:

for (var i = 0; i < d.length; i++) {
    $scope.coutas.push(d[i].coutas); // if need to parse then use JSON.parse(d[i].coutas)
}

No need to require two loop ,if u did then define $scope.coutas =[ ] and push the arrar of every looping item; 不需要两个循环,如果您确实定义了$ scope.coutas = []并推送每个循环项的arrar;

   var coutas = [];

  for (var i = 0; i < d.length; i++) {
    coutas.push(JSON.parse(d[i].coutas));
  }
$scope.coutas =[];
 for (var i = 0; i < coutas.length; i++) {
    $scope.coutas.push(coutas[i]);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM