【问题标题】:$state.go redirects to old URL not changing the state$state.go 重定向到不改变状态的旧 URL
【发布时间】:2014-10-02 18:45:07
【问题描述】:

我正面临 state.go 无法按预期工作的问题

以下是各种状态的定义方式

(function (app) {
  'use strict';

  app.ng.requires.push('ui.bootstrap');
  //app.ng.requires.push('ngGrid');
  app.ng.requires.push('ui.router');
  app.ng.requires.push('ngTouch');
  app.ng.requires.push('ngAnimate');

  app.ng.run(
  ['$rootScope', '$state', '$stateParams',
  function ($rootScope, $state, $stateParams) {
    debugger;
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
  }
  ]
  );
  app.ng.config(
  ['$stateProvider',
  function ($stateProvider) {
    debugger;
    var modulePath = 'modules/listBuild';
    var ctrlRoot = app.root + modulePath + '/Views/';   
    $stateProvider
    .state('recipe',
    {
      url: '#recipe',
      templateUrl: ctrlRoot + 'selectRecipe.html'
    })
    .state('selectLocation',
    {
      url: '#selectLocation',
      templateUrl: ctrlRoot + 'selectLocation.html'
    })
 }]);

}(window.app));

点击事件使用 state.go 改变视图

scope.goToListbuild = function () {             
            timeout(function () {
                location.path('/' + sessionSvc.get('clientKey') + '/' + sessionSvc.get('orgKey') + '/lists/build');
                state.go('recipe');         
            }, 10);
};

现在,当会话值发生变化时,用户应该被重定向到不同的路径。当前发生的情况是,即使会话值更改了 state.go 也会将其转换为旧 URL。

我尝试了 state.go 和 state.reload 的不同选项,但它不起作用

因此,如果这是主页“/PQR/PQR-sec1/lists”的更改 URL,我单击链接时它实际上会返回到“/ABC/ABC-sec1/lists/build#recipe”而不是“ /PQR/PQR-sec1/lists/build#recipe"

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    我认为你为你的状态定义了一个错误的 url,例如:

    $stateProvider
      .state('recipe',
      {
         url: '/recipe', //url which looks like myDomain.com/#/recipe 
         templateUrl: ctrlRoot + 'selectRecipe.html'
      })
      .state('selectLocation',
      {
         url: '/selectLocation',
         templateUrl: ctrlRoot + 'selectLocation.html'
      });
    

    但我建议您在 html 中使用“ui-sref”,因为我认为您可以在 onClick 上更改它,所以只需使用类似这样的东西

      <a ui-sref="recipe"> </a>
    

    如果不起作用,请尝试评论您的行 location.path(...)

    【讨论】:

    • 我认为我不能使用 ui-sref,因为它是有条件的,并且在我可以使用 state.go() 之前必须使用更改的位置......我真正想要的知道是否有什么可以重新加载路径
    • 我用 window.location.href 更改了 location.path 并且它起作用了,但我真的不认为我会使用那个 hack,但它会帮助有人在这行提出一些建议
    • 这对我有帮助 :) 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 2014-12-31
    • 2016-12-20
    • 2017-06-19
    相关资源
    最近更新 更多