2

我有这样的状态定义:

 $stateProvider.state('password', {
    url: '/password',
    templateUrl: 'password.html',
    controller: 'PasswordController',
    controllerAs: 'ctrl',
    params: {
        accountNumber: {
            value: null,
            squash: true
        }
    },               
});

我需要用它href而不是使用ui-serif指令来引用这个状态,我怎样才能params.accountNumber在href里面设置?

这不起作用:

$state.href('password', {accountNumber: $scope.accountNumber})

当我将此行更改url: '/password',为 `url: '/password?accoutNumber',并删除这部分

params: {
    accountNumber: {
        value: null,
        squash: true
    }
}, 

$state.href工作得很好。

4

1 回答 1

3

为了能够生成 URL$state.href并传递参数...

  • 此类参数必须是url定义的一部分
  • 仅在其中声明它params: {}会导致 href - 不包含它,不传递

有调整的状态定义:

  .state('password', {
    url: '/password/:accountNumber',
    templateUrl: 'password.html',
    controller: 'PasswordController',
    controllerAs: 'ctrl',
    params: {
        accountNumber: {
            value: null,
            squash: true
        },
      },               
  });

一个工作的plunker

原始部分,与错字有关

$state.href()调用似乎没问题......只是状态定义可能是错误的:

$stateProvider.state('password', {
    //Url: '/password',
    url: '/password',
    ...

url设置区分大小写。请参阅此处的 state.href 文档

href(状态或名称,参数,选项)

于 2016-08-29T08:47:04.003 回答