【问题标题】:How to redirect in angularjs如何在angularjs中重定向
【发布时间】:2013-02-12 20:20:39
【问题描述】:

这可能是一件微不足道的事情,但我是一个 angularjs 新手。 这是我的 angularjs 控制器代码

    function MyCtrl1($scope, $location, $rootScope) {
  $scope.$on('$locationChangeStart', function (event, next, current) {
    event.preventDefault();
    var answer = confirm("Are you sure you want to leave this page?");
    if (answer) {

    }
  });
}
MyCtrl1.$inject = ['$scope', '$location', '$rootScope'];

next 变量中,我有要重定向的url,确认OK。但是如何在angularjs 中完成这件事。

【问题讨论】:

    标签: javascript redirect angularjs angularjs-directive


    【解决方案1】:

    您不必手动执行此操作。只有在他们确认时才取消活动:

    $scope.$on('$locationChangeStart', function (event, next, current) {
        if ( ! confirm("Are you sure you want to leave this page?") ) {
            event.preventDefault();
        }
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用其他答案中提到的纯 javascript 或者你可以像这样使用 Angular 提供的 $location 服务

          $location.path(next)
      

         $location.replace(next)
      

      第一个添加到您当前的路径(大部分是部分路径)

      第二个替换当前路径(如 google.com 到 yahoo.com)

      【讨论】:

        【解决方案3】:

        window.location = 'myURL'

        Here's the MDN docs

        【讨论】:

        猜你喜欢
        • 2016-06-26
        • 2016-12-12
        • 1970-01-01
        • 2013-07-04
        • 1970-01-01
        • 2016-07-17
        • 2016-11-21
        • 2015-06-10
        • 1970-01-01
        相关资源
        最近更新 更多