将routeParams值获取到Angular中的控制器中

时间:2016-05-04 18:04:43

标签: angularjs routes

我无法将routeParams转换为Angular控制器。

app.controller("ReplicatedCtrl", function(ReplicatedService, $routeParams) {
  var replicated = this;

  replicated.myPhone = $routeParams.myPhone;
  console.log(replicated.myPhone); // logs undefined

我的$ roteProvider:

$routeProvider
    .when('/:myPhone', {
        templateUrl: function(attrs){
            console.log(attrs.myPhone);
            return 'pages/8week/8week-page.tmpl.html';
        },
        controller: 'EightWeekCtrl',
        controllerAs: 'eight'
    });

主控制器与我尝试将此值传递给的控制器不同。我认为$ routeParams的功能与$ scope非常相似。它是否仅与分配给路径的控制器隔离?

1 个答案:

答案 0 :(得分:-1)

我认为你所寻找的是$ state value,它返回一个对象并从那里看你需要什么。确保将其包含在您的控制器中,如下所示。如果是这样,请告诉我,否则我可以更新。

function ExampleCtrl($state) {
  'use strict';

  var ctrl = this;

  console.log($state);
}

// Should output something like this.
Object {
  params: Object,
  current: Object,
  $current: extend,
  transition: null
}
$current
  :
  extend
current
  :
  Object
get: (stateOrName, context)
go: go(to, params, options)
href: href(stateOrName, params, options)
includes: includes(stateOrName, params, options)
is: is(stateOrName, params, options)
params: Object
reload: reload(state)
transition: null
transitionTo: transitionTo(to, toParams, options)
__proto__: Object