【问题标题】:Unable to reflect $http response in the view ng-repeat无法在视图 ng-repeat 中反映 $http 响应
【发布时间】:2016-03-19 14:04:02
【问题描述】:

我已经看到了与此相关的其他问题,并根据我的理解实施了答案。但是,我无法使用 ng-repeat 在视图中显示响应。这是我的代码。

JS 文件:

app.controller('testcontrol', ['$scope', '$http', function($scope, $http) {
    $scope.resultset = [];
    $http.get('http://localhost:3000/testdata').then(function(data) {
      $scope.resultset = data.data.resultset;
      console.log($scope.resultset);
      },function(error) {
        $scope.error = error;
      })
}]);

HTML:

<div class="wrapper-md" ng-controller="testcontrol">
    <ul>
        <li data-ng-repeat="result in resultset">
            {{result.name}}
        </li>
    </ul>
</div>

我可以在控制台中看到数据。但是,我无法在视图中看到这种情况。我不明白为什么不。我正在使用 promise 并初始化变量,这样 ng-repeat 就不会阻塞。

控制台响应:

[Object, Object, Object, Object, Object]
0: Object
avatar: "img/a4.jpg"
followers: "2104"
name: "Chris Fox"
title: "Master Chef"
url: "#"
__proto__: Object
1: Object
2: Object
3: Object
4: Object
length: 5
__proto__: Array[0]

【问题讨论】:

  • 可能你得到的数据结构与你预期的不同。使用控制台日志更新您的问题。
  • 我期望得到的数据结构是一个数组。这不是数据结构的问题。无论如何,我正在使用控制台响应更新问题。
  • 我认为在分配后使用 $scope.$apply() 可以解决问题

标签: angularjs ng-repeat angular-http


【解决方案1】:

这是由于范围问题。在 Angular 中,您不能直接交换对象或数组。

要使其工作,请使用angular.copy

app.controller('testcontrol', ['$scope', '$http', function($scope, $http) {
    $scope.resultset = [];
    $http
      .get('http://localhost:3000/testdata')
      .then(function(data) {
        angular.copy(data.data.resultset, $scope.resultset); // HERE
        console.log($scope.resultset);
      }, function(error) {
        $scope.error = error;
      });
}]);

更新

我终于找到了the documentation。它有点冗长,但对于理解 AngularJS 1.x 来说是必不可少的。我建议从上到下阅读文档:)

【讨论】:

  • 没有。不是这个。正如我所提到的,控制台响应符合预期。所以,数组赋值没有问题。
  • @Aravind 你的数据没问题。问题是您不能直接将数组分配给$scope.resultset,因为它会更改该数组的引用。在这种情况下,您需要使用 angular.copy 来保留其引用。 (我仍在挖掘文档...)
  • 我已经尝试过您的解决方案。不过,视图中没有任何内容。
  • @Aravind 它在这个plunker 中工作。
  • 嘿,感谢您的支持。它正在工作,这是一个范围界定问题,而不是代码问题。你的 plunker 帮我解决了这个问题。
【解决方案2】:

在作用域函数或私有函数中使用您的分配或操作。因此,这将帮助您仅在需要时调用该方法,并且您可以重复使用相同的方法。

注意:确保您获取的是数组结果中的数据。

app.controller('testcontrol', ['$scope', '$http', function($scope, $http) {
    $scope.resultset = [];
    $scope.GetData =function(){
       $http.get('http://localhost:3000/testdata').then(function(data) {
          $scope.resultset = data.data.resultset;
          console.log($scope.resultset);
      },function(error) {
        $scope.error = error;
      })
    }

    $scope.init=function(){
        $scope.GetData();
    }
}]);

HTML:

<div class="wrapper-md" ng-init="init()" ng-controller="testcontrol">
    <ul>
        <li data-ng-repeat="result in resultset">
            {{result.name}}
        </li>
    </ul>
</div>

【讨论】:

  • ngInit 不应以这种方式使用。请参阅其documentation中的警告
  • 我同意@ParkSoonWai,但它还是没有用。
  • @ParkSoonWai 不使用init,我可以调用该方法。所以它可以调用 getData 并获取数据。但有时脚本会被加载两次。因此将发生不必要的两次调用。我见过他们的cmets。我们是否需要将 ng-init 仅用于 ng-repeat 作为别名属性???
  • @JeevaJsb 技术上ngInit 很适合进行初始化调用。问题是控制器逻辑现在位于模板中。 “模板初始化控制器”的想法听起来不太对。通过控制器自己初始化,逻辑流程感觉更好。此外,在测试方面,ngInit 使控制器更难测试。您最终会为控制器测试创建模拟模板,而这本来是不必要的。
猜你喜欢
  • 1970-01-01
  • 2023-03-12
  • 2014-06-12
  • 2019-05-15
  • 2015-05-13
  • 2019-12-16
  • 2018-05-04
  • 1970-01-01
  • 2019-03-01
相关资源
最近更新 更多