Angularjs Foreach数组仅返回Html

时间:2016-06-10 07:34:32

标签: html angularjs foreach

我是angularjs的新手。我试图显示从数据库中获取的foreach值。这是我的示例代码

 $scope.listdb = function() {
                    var response = $http.get(" http://192.168.1.100:8000/demo");
     response.success(function(data, status, headers, config) {
                    console.log("SUCCESS, Response from db");
                    console.log("SUCCESS DATA: ", data);
                    $scope.Data = data;
                      var stuff = [];
                    angular.forEach( $scope.Data, function(value, key) {
      console.log(key + ': ' + value); 
      var post_data = { "value": 1,
                "id":  value, "column":"name"};
                $http({method: 'POST',
                      url: 'http://192.168.1.100:8000/view_column',
                      data: post_data,
                       headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                        'Access-Control-Allow-Origin' : '*',
                        'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS'
        }
    })
       .success(function(data, status, headers, config) {
        $scope.viewdata=data;
        var stuf = [];
                    angular.forEach( $scope.viewdata, function(value, key) {
                        console.log(key + ': ' + value);
                        $scope.stuff=value;
                        stuf.push($scope.stuff);
                        console.log($scope.stuff);

                    })
                    $scope.st=stuf;
                  })
                    .error(function(data, status, headers, config) {
                        console.log("POST ERROR", data);
                     });ff);
                    });
                    response.error(function(data, status, headers, config) {
    console.log("error");                    
    alert(" failed!");
                    });
     });
           };

输出: 数据 - 11,23,43,56 viewdata - name1,name2,name3,name4

当我执行上面的代码时,我按预期在控制台中获取输出

controller.js:47 name1
controller.js:46 Result: name1
controller.js:47 name2
controller.js:46 Result: name2
controller.js:47 name3
controller.js:46 Result: name3
controller.js:47 name4
controller.js:46 Result: name4

但是当我在Html中显示值时,它只返回最后一个值" name4"

 <tr ng-repeat="value in st" >
 <td>{{value}}</td> </tr>

我的查询是我必须在Html中显示4个值(name1,name2,name3,name4)。

0 个答案:

没有答案