$ state.href的ui-sref params选项未按预期工作

时间:2016-08-29 08:43:09

标签: angularjs angular-ui-router directive ui-sref

我有一个像这样的州定义:

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

我需要使用href而不是使用ui-serif指令来引用此状态,如何在href中设置params.accountNumber

这不起作用:

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

当我将此行url: '/password',更改为“url:' / password?accoutNumber'”时,请删除此部分

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

$state.href效果很好。

1 个答案:

答案 0 :(得分:2)

能够使用 $state.href 生成网址并传递参数...

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

有调整后的州定义:

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

a working plunker

ORIGINAL部分,与拼写相关

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

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

url设置区分大小写。见state.href doc here

  

href(stateOrName,params,options)