Angularjs 当$location.path(';new/path';)下面有代码时会发生什么情况?

Angularjs 当$location.path(';new/path';)下面有代码时会发生什么情况?,angularjs,Angularjs,如果我执行一个路径更改,比如$location.path('new/path'),那么这个路径更改下面的代码会发生什么情况?据我所知,路径更改不会停止其余代码的运行,但实际发生了什么?代码完成后,路径才会改变吗?因此,如果这段代码非常耗时(比如说在慢速网络连接上将某些内容保存到服务器),那么位置更改是否也会同样延迟?或者事情会并行发生吗?,看起来它完成了代码的运行,然后更改了位置。看看JSFIDLE中的控制台,您将看到两个循环都在运行,但是一个接一个 正如@SilverlightFox指出的,j

如果我执行一个路径更改,比如
$location.path('new/path')
,那么这个路径更改下面的代码会发生什么情况?据我所知,路径更改不会停止其余代码的运行,但实际发生了什么?代码完成后,路径才会改变吗?因此,如果这段代码非常耗时(比如说在慢速网络连接上将某些内容保存到服务器),那么位置更改是否也会同样延迟?或者事情会并行发生吗?

,看起来它完成了代码的运行,然后更改了位置。看看JSFIDLE中的控制台,您将看到两个循环都在运行,但是一个接一个

正如@SilverlightFox指出的,javascript中没有并行处理

//this one loads first, executes the loop, then changes location. 
function HomeCtrl($scope, $location) {
    $location.path('/about');
    var i  = 0;
    while(i < 1000000000) {
        if(i % 100000000 === 0) {
            console.log('home')   
        }
       i++;
    }
}

//this loop executes second. 
function AboutCtrl($scope) {
    $scope.name = 'John';
     var i  = 0;
    while(i < 1000000000) {
        if(i % 100000000 === 0) {
            console.log('about')   
        }
        i++
    }
}
使用与超时相同的参数:位置更改,“about”循环运行,然后http请求完成。因此,在慢速连接上更新数据库的ajax调用不会阻止应用程序更改路由

//changes location, executes 'AboutCtrl', then finishes the http request and 
//executes the 'home' loop.
function HomeCtrl($scope, $location, $http) {
    $location.path('/about');
    $http.get('/echo/json/').success(function() {
        var i  = 0;
        while(i < 1000000000) {
            if(i % 100000000 === 0) {
                console.log('home')   
            }
           i++;
        }        
    });
//更改位置,执行'AboutCtrl',然后完成http请求并
//执行“主”循环。
函数HomeCtrl($scope、$location、$http){
$location.path('/about');
$http.get('/echo/json/').success(function(){
var i=0;
而(i<100000000){
如果(i%100000000==0){
console.log('home')
}
i++;
}        
});

如果因为网络调用而更改路由,我会在成功/错误承诺或回调中更改路由。

不会有任何并行处理,因为JavaScript是单线程的。您可以发出异步请求,但任何触发的事件都会排队。@SilverlightFox是的,您是对的。我是更多地考虑事情的顺序。如果足够快的话,
$location
可以在函数的其余部分执行之前更新路径。
//changes location, executes 'AboutCtrl', then finishes the http request and 
//executes the 'home' loop.
function HomeCtrl($scope, $location, $http) {
    $location.path('/about');
    $http.get('/echo/json/').success(function() {
        var i  = 0;
        while(i < 1000000000) {
            if(i % 100000000 === 0) {
                console.log('home')   
            }
           i++;
        }        
    });