【问题标题】:Why ng-repeat returns only one value of a list twice?为什么 ng-repeat 只返回列表的一个值两次?
【发布时间】:2020-11-11 16:26:33
【问题描述】:
  <div class="row" ng-repeat="product in products">
                <div class="col-sm-12 col-md-3">
                    <div class="form-group">
                            <input type="text"  name="warehouseName"
                                   ng-model="warehouseName"
                                   ng-readonly="true"/>
                    </div>
                </div>
     ....

    </div>                           

   $scope.getAllProductsByWarehouseId = function() {
      
        productService.getAllProductsByWarehouseId($scope.product.id).$promise
            .then(function (response) {
                $scope.products = response;
                 console.log("response", $scope.products[0]);
                 console.log("response", $scope.products[1]);

                for(let i=0; i<=$scope.products.length; i++) {
                    $scope.products = response;
                    console.log("response", $scope.products[i]);//However I have a list, this return one value
                    this.products.push(response);
                }}).catch(function(e) {
                    console.log('Error: ', e);
                    throw e;
                }).finally(function() {
                    console.log('This finally block');
                });

                }

这是我项目的一部分。当对象产品是具有 3 个对象的列表时。带有控制台日志 console.log("响应", $scope.products[0]); console.log("响应", $scope.products[1]); 我可以看到物体..正确!!!
但是在网络浏览器中,我得到了两次相同的对象..有什么想法可以纠正它吗?

【问题讨论】:

    标签: angularjs angularjs-ng-repeat


    【解决方案1】:

    不确定我是否完全理解您的问题,但您将 api 响应分配给 $scope.products 两次。

                 $scope.products = response; //here you assign it after the promise
                 console.log("response", $scope.products[0]);
                 console.log("response", $scope.products[1]);
    
                for(let i=0; i<=$scope.products.length; i++) {
                    $scope.products = response; // and again here.
                    console.log("response", $scope.products[i]);//However I have a list, this return one value
                    this.products.push(response); // not sure what this does
                }
    

    【讨论】:

      【解决方案2】:

      您的代码中有几个问题,主要问题是您将响应分配给 $scope.products,这看起来没有必要。您应该使用数组初始化 $scope.products,并始终将项目推入其中,而不是覆盖整个列表。

      $scope.getAllProductsByWarehouseId = function() {
          $scope.products = []
          productService.getAllProductsByWarehouseId($scope.product.id).$promise
          .then(function (response) {
              for(let i=0; i<=$scope.products.length; i++) {
                  // $scope.products = response;
                  console.log("response", $scope.products[i]);//However I have a list, this return one value
                  this.products.push(response);
              }}).catch(function(e) {
                  console.log('Error: ', e);
                  throw e;
              }).finally(function() {
                  console.log('This finally block');
              });
      
              }
      

      【讨论】:

        猜你喜欢
        • 2017-05-13
        • 1970-01-01
        • 2013-08-24
        • 1970-01-01
        • 1970-01-01
        • 2017-09-02
        • 2021-04-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多