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 延迟路径改变_Javascript_Angularjs - Fatal编程技术网

Javascript 延迟路径改变

Javascript 延迟路径改变,javascript,angularjs,Javascript,Angularjs,我是AngularJS的新手,这是我的尝试: $scope.comparisonlist.forEach(function (script) { $http.get('/info/' + script.id) .success(function (data) { $scope.comparisonResultList.push(data) }) .error(function (data, statu

我是AngularJS的新手,这是我的尝试:

$scope.comparisonlist.forEach(function (script) {
     $http.get('/info/' + script.id)
         .success(function (data) {
              $scope.comparisonResultList.push(data)
          })
          .error(function (data, status) {
                console.error('Repos error', status, data);
           })

});

 $location.path('/comparison');
我试图在http请求之后(在请求循环之后)更改路径,但是发生的是在http请求完成之前更改路径。我不知道如何解决这个问题。我试着使用promise,但它完全不起作用

  <div ng-repeat="result in comparisonResultList">


有了promise,当我执行此操作时^^^我什么也没有得到,我假设它正在检测comparisonResultList是否为空

当前promise实现应该可以工作,在forEach循环中的每个异步操作完成后调用路径更改

var promises = [];
$scope.comparisonlist.forEach(function (script) {
  promises.push($http.get('/info/' + script.id)
    .success(function (data) {
      $scope.comparisonResultList.push(data)
    })
    .error(function (data, status) {
      console.error('Repos error', status, data);
    })
  )
});
$q.all(prom).then(function () {
  $location.path('/comparison');
});

请求在循环中发生。我希望完成所有请求,然后执行$location.path('/comparison')@安德森教授,直到现在才看到。编辑我的答案,我认为$q是最好的方法(不知道它以前是如何/为什么破坏了你的程序)这一个破坏了。。。当我说break时,我的意思是,在我的html中,我正在执行ng repeat,但如果将一个数据数组硬编码到
$scope.comparisonResultList
,它是否有效?如果没有,则会发生其他情况。我得到的最后一件事是尝试强制视图重新渲染:调用
$scope.$apply()在路径更改之后。