代码之家  ›  专栏  ›  技术社区  ›  Daine MacTavish

我的ng repeat只显示最新的循环

  •  0
  • Daine MacTavish  · 技术社区  · 8 年前

    我想显示数组中的所有内容,但它只显示最新的循环。以下是我的控制器功能:

    /*global angular*/
    var app = angular.module('statisticsApp', []).controller('myCtrl',
    function ($scope, $http) {
    "use strict";
    return $http({
        method : "POST",
        url : "GatewayAPI.php",
    
    }).then(function mySuccess(response) {
        $scope.records = response.data;
    
        var mydata,myJSON,myresult,myjava, myobj;
        var i;
        var Result; 
        var chartResultTemp = [];
        var resultType = [];
    
        for(i=0; i<72;i++)
        {
            //storing data
            mydata = $scope.records.data[i];
    
            //retrieving data
            Result = mydata.data.substring(6,9); //throw this in 
    
            myobj = mydata.data.substring(3,4);
            resultType = mydata.data.substring(3, 4);
    
            if(resultType === "A") { //selects type = a
                chartResultTemp = mydata.data.substring(6,9);
            } ;
    
            $scope.test2=chartResultTemp; //this one
            $scope.test3 = resultType;
    
            console.log(Result);
            console.log(resultType);
        }
    
    
    
    
        $scope.gotTemp = false;
        $scope.gotHumidity = false;
        $scope.getSoilMoisture = false;
    
    });
    
    });
    

    这是php中的代码,我在其中列出了数组中的所有项:

    <ul>
      <li data-ng-repeat="cd in test2">{{cd}}</li>
    </ul>
    

    这是我在浏览器控制台中查看的当前结果: enter image description here

    从控制台日志中可以看到,它只显示最新的循环。我想显示数组中的所有数据。是因为我错误地声明了数组吗。

    更新:

    所以我用了 $scope.test2.push(chartResultTemp) ,它给了我一个错误:无法读取未定义的属性“push”。

    然后,我试着

    if(resultType === "A") { 
       chartResultTemp.push([mydata.data.substring(6,9)]);
    } ;
    

    它是有效的,但问题是列出的项目中有括号。 enter image description here

    2 回复  |  直到 8 年前
        1
  •  1
  •   Sajeetharan    8 年前

    你需要进入阵列

    $scope.test2.push(chartResultTemp);
    
        2
  •  1
  •   Mamun    8 年前

    通过使用( = )在每次迭代中,为同一范围分配不同的值,最终保留分配给它的最终值。要存储所有值,必须创建一个数组并 push 每个迭代的值。

    $scope.test2 = [];
    for(let i=0; i<72; i++){
      .....
      .....
      $scope.test2.push(chartResultTemp);
      .....
      .....