【问题标题】:$location.path is not working$location.path 不工作
【发布时间】:2013-08-11 04:17:52
【问题描述】:

当用户输入正确的用户名和密码时,我想重定向到另一个位置。但是当我在这里使用$location.path('dashboard') 时,浏览器的 URL 已更改,但该页面未加载。当使用 ctrl+R 刷新我的页面或单击浏览器的刷新图标时,将加载相应的页面。

$http.post('/login', $scope.userInfo)
        .success(function (data) {
            //Check if the authentication was a success or a failure
            //Successful POST here does not indicate success of authentication
            if (data.status === "authentication success") {

                //Proceed to load the dashboard for the user                    
                $location.path('/dashboard');

            } else if (data.status === "authentication error") {
                //Inform user of the error
                $scope.errorMessage = data.error;
                $scope.showErrorMessage = true;
            }

        })
        .error(function (error) {
            $scope.errorMessage = "Error while attempting to authenticate. Check  log.";
            $scope.showErrorMessage = true;

        });
    };

}]);

【问题讨论】:

  • 在那之后你有没有尝试过使用 $scope.$apply ?例如。 if(!$scope.$$phase) $scope.$apply()
  • @RafalPastuszak 你的回答对我有用。请把它作为一个单独的答案提出来
  • @davneet 完成,谢谢。

标签: angularjs


【解决方案1】:

使用 $timeout(function){} 这将起作用

【讨论】:

  • 感谢您的建议
【解决方案2】:

试试这个(别忘了注入 $timeout):

$timeout(function () {
   $location.path('/dashboard'); 
}, 0);

【讨论】:

  • 这是为我做的。使用此方法不需要 $scope.$apply()
【解决方案3】:

引用https://docs.angularjs.org/guide/$location

"$location服务只允许你改变URL;它不允许你重新加载页面。当你需要改变URL并重新加载页面或导航到不同的页面时,请使用较低级别的API , $window.location.href。”

因此,例如,不要使用:$location.path("/path/to/something");

使用这个:$window.location.href = "/path/to/something";

$window.location.href 将更改网址加载新页面。

希望对您有所帮助。

【讨论】:

  • 在我的情况下,我无法让它与 $window 一起使用,所以我只使用了 'window.location.href'。
  • 是的,我想这只是window.location.href。使用 window.location.href 是正确的解决方案——非常感谢。
  • 要使用$window,请务必将其包含在您的函数参数中,如下所示:myApp.controller('myCtrl', function($window, $location){...})
【解决方案4】:

使用$rootScope.$evalAsync

例如:

$rootScope.$evalAsync(function() {
     $location.path('/dashboard'); 
});

【讨论】:

    【解决方案5】:

    如果你没有 $scope(例如服务) 那么你可以使用

     $location.path('/path');
     if (!$rootScope.$$phase) $rootScope.$apply();
    

    【讨论】:

    • 没有其他东西对我有用,但这确实有效。谢谢! :-]
    • 太好了。在我的错误场景中完美地为我工作。 (从我的控制器内部的外部库运行第 3 方处理函数,需要强制作用域)。
    【解决方案6】:

    试试:

     $location.path('/dashboard');
     $location.reload();
    

    【讨论】:

    • .reload() 不是 $location 对象的有效方法
    【解决方案7】:

    之后您是否尝试过使用 $scope.$apply?例如:

    if(!$scope.$$phase) $scope.$apply()
    

    这个简短的方法也经常派上用场:

    https://github.com/yearofmoo/AngularJS-Scope.SafeApply

    【讨论】:

    • @ShankarKamble 如果对您没有帮助,为什么要接受答案?
    • 请不要盲目使用$scope.$apply而不理解为什么。它很少需要或有用,因为 Angular 会自动在摘要中为您执行异步回调。需要应用通常意味着您不必要地跳出了 Angular 框架,例如使用 jQuery.ajax 代替 $httpsetTimeout 代替 $timeoutonclick 代替 ng-click 或其他一些异步来自 Angular 外部的回调,Angular 包装器已经存在。
    • @MarkAmery 是的。我发现自己在这里是因为 setTimeout。只是不知道 $timeout 存在。
    • @MarkAmery 非常有用的评论
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    相关资源
    最近更新 更多