【问题标题】:angular location redirect still runs the current controller, how to?角度位置重定向仍然运行当前控制器,如何?
【发布时间】:2014-08-08 19:03:40
【问题描述】:

我正在尝试从一个页面重定向到另一个页面。每个页面都有自己的控制器。

我愿意

.controller('HomeCtrl', function ($scope, $location) {
    $scope.settings = Helper.getLS('settings');
    if (!$scope.settings) $location.path("/app/settings");
    console.log('why am i seeing this?');

...

网址正在更改,但问题是我仍然可以在控制台中看到日志,并且此页面中的其余功能仍在运行

我想停止执行并重定向并在新页面上运行另一个控制器

有什么想法吗?

【问题讨论】:

  • 你需要return; 就行了if (!$scope.settings) return $location.path("/app/settings");
  • 该死,你是对的。您可以发布回复并不接受它

标签: javascript angularjs location url-redirection


【解决方案1】:

设置完路径后需要退出函数,以免其余代码形式被执行。 $location.path() 不会停止代码并重定向。所以就这样做吧:-

      if (!$scope.settings) {
         return $location.path("/app/settings");
      }
      //Rest of the code

【讨论】:

    猜你喜欢
    • 2016-06-16
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多