在Angular UI路由器中发送多个参数而不指定url

Sending multiple params in the Angular UI Router without specifying a url?

本文关键字:参数 url UI Angular 路由器      更新时间:2023-09-26

我有一个具有两个状态的应用程序。State1没有模态对话框,而State2有一个自定义模态对话框。如果我在State2模态窗口打开时单击浏览器返回按钮,它将带我回到之前的状态State1。我不希望这样(因为如果多次打开和关闭模态,浏览器将返回所有这些)。移除UI路由器中的URL解决了这个问题,但我还有另一个问题。

我需要通过状态发送多个参数,但显然它只允许我发送一个。我的状态如下:

.state('state2',{
    params: {id: NaN, type: "empty"},
    views:{
        'modalContent':{
            templateUrl: 'templates/modal-content-super-region.html',
            controller: 'ModalStatesCtrl'
        }
    }
})

和下面的指令:

<a ui-sref="state2({id: 1, type: 'someStringHere'})">The link</a>

当我控制日志$stateParams时,我得到以下内容:

{ id: 1, type: "empty"}

所以指令正确地传入了id,但根本没有设置类型,传入的类型仍然保持默认值。有什么我能做的吗?没有URL(我肯定不能有),我只能传递一个状态参数吗?

有一个工作柱塞。我不得不说,看起来,你的例子,你的期望是有效的。可能只是代码中的一些错别字…

所以,不变的state2和state1(与url看到一些生成的href)

// just to have state with standard <a> generated - url is needed
.state('state1', {
  // url is supporting href being generated
  url: "/state1",
  params: {
    id: NaN,
    type: "empty"
  },
  views: {
    'modalContent': {
      templateUrl: 'templates/modal-content-super-region.html',
      controller: 'ModalStatesCtrl'
    }
  }
})
// unchanged state def
.state('state2', {
  params: {
    id: NaN,
    type: "empty"
  },
  views: {
    'modalContent': {
      templateUrl: 'templates/modal-content-super-region.html',
      controller: 'ModalStatesCtrl'
    }
  }
});

,这是它们被调用和传递参数的方式:

<a ui-sref="state1({id: 1, type: 'someStringHere'})">
<a ui-sref="state2({id: 22, type: 'someOtherHere'})">

点击这里