Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ng重复列表没有';t在api调用后立即更新_Javascript_Angularjs_Google Drive Api_Angularjs Ng Repeat - Fatal编程技术网

Javascript ng重复列表没有';t在api调用后立即更新

Javascript ng重复列表没有';t在api调用后立即更新,javascript,angularjs,google-drive-api,angularjs-ng-repeat,Javascript,Angularjs,Google Drive Api,Angularjs Ng Repeat,“我的web应用”视图在嵌套列表中列出文件夹及其子文件夹: <ul> <li class="folderLi" ng-repeat="folder in folders"> <a href="" ng-click="listFolderFiles( folder )">{{folder.title}}</a> <ul > <li ng-repeat="child in folder.children

“我的web应用”视图在嵌套列表中列出文件夹及其子文件夹:

<ul>
  <li class="folderLi" ng-repeat="folder in folders">
    <a href="" ng-click="listFolderFiles( folder )">{{folder.title}}</a>
    <ul >
      <li ng-repeat="child in folder.children">
          {{child.title}}
      </li>
    </ul>
  </li>
</ul>

此函数可正确检索信息并更新模型。但是,子项的ng重复列表不会立即更新和显示。相反,它会在下次单击文件夹时更新和显示。似乎此函数正在api调用完成之前更新视图,因此下次函数运行时,视图将更新以显示上次api调用的响应。想法?

您试图更新angular以外的知识。因此,您的视图不会更新。您需要使用
$scope.$apply()
$timeout
来运行摘要循环

request.then(function(response){
     //log successful response
     console.log(response);
     //folder.children = response.result.items; //Required in case of $scope.$apply()
     $timeout(function(){folder.children = response.result.items;}); //Or $scope.$apply(); (Don't forget to inject $timeout in your controller)
           }, function(response) {
     //log error
      console.log(response);
   })
  }

尝试使用
$scope.$apply()强制执行摘要循环不能将文件夹添加为$scope变量吗?这可能会迫使AngularJS跟踪它。看看这是一个基于AngularJS的库,谷歌驱动AngularJS。目前是早期alpha,但是prob足够稳定,可以使用。问题中的所有代码都将替换为一行folder.children=DriveService.files.list({q:““+folder.id+””,在parents“},true)中)。数据。免责声明:我是author.btw,请注意,您粘贴的代码也将返回垃圾文件。这可能是故意的,但在q属性中更常见的是看到“trashed=false”。感谢您在我问这个问题之前回答我关于如何停止获取垃圾文件的问题。您是对的,但是,在我看来,直接使用$apply不是一个好的做法。我建议将内容包装成$timeout(function(){},0)@奥尔费夫。是的,你是对的,我忘了我也更喜欢
$timeout
。不知道我是怎么错过的。更新日期:)即使是
$timeout
也不是好的做法。使用
$q
包装外部承诺
$q
将在内部触发摘要循环。@Yoshi。只是出于好奇,为什么不
$timeout
?@Zee因为它感觉脏;)不是真的,我只是认为,
$timeout
并不是真的要用于此,它只是一个实现细节,它将对应用程序产生预期的效果。
request.then(function(response){
     //log successful response
     console.log(response);
     //folder.children = response.result.items; //Required in case of $scope.$apply()
     $timeout(function(){folder.children = response.result.items;}); //Or $scope.$apply(); (Don't forget to inject $timeout in your controller)
           }, function(response) {
     //log error
      console.log(response);
   })
  }