无法获得ui-router状态参数

Mis*_*hko 3 angularjs angular-ui-router ionic-framework

考虑以下ui-router配置:

$stateProvider
  .state('app', {
    url: "/app",
    abstract: true,
    templateUrl: "templates/menu.html",
    controller: 'AppCtrl'
  })
  .state('app.feed', {
    url: "/feed",
    views: {
      'menuContent': {
        templateUrl: "templates/feed.html",
        controller: "FeedCtrl"
      }
    }
  })
Run Code Online (Sandbox Code Playgroud)

我的应用中有以下链接:

<a ui-sref="app.feed({ feedId: 5 })">My Link</a>
Run Code Online (Sandbox Code Playgroud)

我正试图获得feedId状态参数:

angular.module('MyApp').controller('FeedCtrl', function($state) {
  console.log($state.params);
});
Run Code Online (Sandbox Code Playgroud)

但不幸的$state.params是,总是如此{}.

我究竟做错了什么?

奖金问题

我希望FeedCtrl每次点击链接时都会初始化,即每次状态更改为app.feed.所以,如果我app.feed多次在状态和其他状态之间切换,我希望console.log上面的内容多次运行.但是,看起来ui-router FeedCtrl仅初始化一次.为什么?

Rad*_*ler 6

要传递参数 - 我们必须定义它:

 .state('app.feed', {
    url: "/feed/{feedId}",
    views: {
      'menuContent': {
        templateUrl: "templates/feed.html",
        controller: "FeedCtrl"
      }
    }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我们将其定义为 {feedId}

在这里查看更多

一些例子:

  • '/user/{id:[^/]*}'- 与'/user/{id}'上一个示例相同.
  • '/user/{id:[0-9a-fA-F]{1,8}}' - 与前面的示例类似,但仅在id参数由1到8个十六进制数字组成时才匹配.
  • '/files/{path:.*}' - 匹配以'/ files /'开头的任何URL,并将路径的其余部分捕获到参数'path'中.
  • '/files/*path' - 同上.捕捉所有的特殊语法.

如果我们确实有参数定义,我们将在具有不同feedId值的状态之间导航......我们将真正看到,该控制器FeedCtrl已重新实例化.这ui-router就是设计的方式