在链接点击时强制重新加载路由/状态

Est*_*ask 5 angularjs single-page-application angularjs-routing angular-ui-router ngroute

目前我正在做这样的事情

link.on('click', function () {
    if (link.attr('href') !== $route.current.originalPath)
        return;
    $route.reload();
});
Run Code Online (Sandbox Code Playgroud)

我不知道副作用,但我想可能有一些.

有没有更简单的方法来处理ngRoute中的这种情况,例如通过$location

当应用程序更新为使用它时,在UI路由器中执行相同操作的方法是什么?

Rad*_*ler 7

使用UI-Router,我们有一组选项,其中之一就是 {reload: true}

go(to,params,options)

  • location - {boolean=true|string=} - 如果为true将更新位置栏中的URL,如果为false则不会.如果是string,则必须是"replace",这将更新url并替换上一个历史记录.
  • inherit - {boolean=true}, 如果为true将从当前url继承url参数.
  • relative - {object=$state.$current},当使用相对路径(例如'^')进行转换时,定义哪个状态是相对的.
  • notify - {boolean=true},如果为true将广播$ stateChangeStart和$ stateChangeSuccess事件.
  • reload (v0.2.5) - {boolean=false},如果true将强制转换,即使状态或参数没有改变,也就是重新加载相同的状态.它与reloadOnSearch不同,因为当你想在一切都相同时强制重新加载时你会使用它,包括搜索参数.

所以我们可以强制状态重载:

$state.go("stateName", stateParams, {reload: true});
Run Code Online (Sandbox Code Playgroud)


Pan*_*kar 1

您的代码可以转换为以下$route.reload()更改$state.reload()

代码

link.on('click', function () {
    if (link.attr('href') !== $route.current.originalPath)
        return;
    //though this will not reload the controller content
    $state.reload(); //will force to reload state..
    $scope.$apply(); //needed here to tell angular js to run digest cycle.
});
Run Code Online (Sandbox Code Playgroud)

git-hub 问题来看,似乎$state.reload()重新加载状态,但控制器没有重新实例化。为此,您需要使用下面的代码而不是$state.reload()

$state.transitionTo('tab.locations', $state.$current.params, { 
  reload: true, inherit: true, notify: true 
});
Run Code Online (Sandbox Code Playgroud)