【问题标题】:Bindonce with ng-repeat使用 ng-repeat 绑定
【发布时间】:2019-01-09 19:32:06
【问题描述】:

我使用Bindonce 来提高ng-repeat 的性能。

但我有一个问题:从ng-repeat 使用的集合稍后会填充数据(从 API 请求数据需要一些时间),所以它是空的,因为Bindonce 阻止了更新。

如何指定等待服务器响应然后进行绑定?


代码示例:

在控制器中我有数组$scope.requests = [];

用工厂初始化

$scope.requests = CurrentUserData.getRequests();

我对承诺感到不满,并认为这段代码可以提供帮助:

CurrentUserData.getRequests()
            .then(function(response) {
                $scope.requests = response;
            });

但我收到一个错误

angular.js:11655 TypeError: CurrentUserData.getRequests(...).then 不是函数

【问题讨论】:

标签: angularjs angularjs-directive angularjs-ng-repeat bindonce


【解决方案1】:

最可能的原因:CurrectUserData 中的函数 getRequests 没有返回承诺,它应该是 return $http.get('/the/url/etc')

【讨论】:

    【解决方案2】:

    CurrentUserData.getRequests(...) 没有返回承诺。

    如果您将使用代码

    CurrentUserData.getRequests()
                .then(function(response) {
                    $scope.requests = response;
                });
    

    那么$scope.requests = response应该改为$scope.requests = response.data

    话虽如此,您可以使用原始代码

    $scope.myData = CurrentUserData.getRequests();
    $scope.$watch(myData,
                  function(newVal, oldVal){
                         $scope.requests = newVal
                  });
    

    【讨论】:

      猜你喜欢
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多