简体   繁体   English

AngularJS-使用UI-Router从父控制器访问$ statParams

[英]AngularJS - Access $statParams from a parent controller with UI-Router

I have a rather complicated prototype app that I have been asked to throw together... now in my app I have some routes / states defined using UI-Router, here is some code from my app.js file, you can see one parent and child route / state: 我有一个相当复杂的原型应用程序,被要求放在一起……现在在我的应用程序中,我使用UI-Router定义了一些路由/状态,这是我的app.js文件中的一些代码,您可以看到一个父对象和子路线/状态:

.state('myapp.loggedInHome.contacts.default', {
        url: '/:profileId', // this could be myurl.com/home/contacts/31333194
        template: '<ui-view/>',
        publicAccess: false
    })
// nested state for contacts profile...
.state('myapp.loggedInHome.contacts.default.news', {
    url: '/news',  // this could be myurl.com/home/contacts/31333194/news
    templateUrl: '/prototype/app/people/views/news.html',
    publicAccess: false,
    controller: 'VisitorsNewsCtrl'
})

Now in the VisitorsNewsCtr l controller I want to access the :profileId as defined in the parent root... however when I try to access this using the $stateParams service I get undefined ! 现在,在VisitorsNewsCtr l控制器中,我想访问父根目录中定义的:profileId ...但是,当我尝试使用$stateParams服务访问此$stateParams我却无法undefined What am I doing wrong in my controller? 我的控制器在做什么错?

.controller('VisitorsCtrl', ['$scope', '$stateParams', 
    function ($scope, $stateParams) {

      console.log($stateParams.profileId); // this is undefined as 
                                           // $stateParams is an empty object

I am redirecting to this state like so... 我正像这样重定向到这种状态...

$state.go('myapp.loggedInHome.contacts.default.news', { profileId: 123456 } );

however the value for profileId is undefined? 但是profileId的值是不确定的? But the value appears in the URL! 但是该值显示在URL中!

You need to add the potential params to the url in your state definition like this: url: '/news/:profileId' 您需要像下面这样在状态定义中向网址添加潜在的参数: url: '/news/:profileId'

If you use $state.go('myapp.loggedInHome.contacts.default.news', {profileId: 1234}) you should then be able to access $stateParams.profileId 如果您使用$state.go('myapp.loggedInHome.contacts.default.news', {profileId: 1234}) ,则应该可以访问$stateParams.profileId

Example definition: 定义示例:

$stateProvider
    .state('contacts.detail', {
        url: "/contacts/:contactId",
        templateUrl: 'contacts.detail.html',
        controller: function ($stateParams) {
            // If we got here from a url of /contacts/42
            expect($stateParams).toBe({contactId: "42"});
        }
    })

I managed to get the value by using the $state object! 我设法通过使用$state对象获取值!

Like so... 像这样

.controller('VisitorsCtrl', ['$scope', '$state', function ($scope, $state) {

console.log($state.params.profileId);

Honestly (despite two other answers) the problem is, that this defintion: 老实说(尽管有其他两个答案),问题是,这个定义:

.controller('VisitorsCtrl', ['$scope', '$stateParams', 
  function ($scope, $stateParams) {

    console.log($stateParams.profileId); 
    ...

Is about controller 'VisitorsCtrl' . 关于控制器'VisitorsCtrl' While the state says 'VisitorsNewsCtrl' : 当状态显示'VisitorsNewsCtrl'

.state('myapp.loggedInHome.contacts.default.news', {
    url: '/news',  // this could be myurl.com/home/contacts/31333194/news
    templateUrl: '/prototype/app/people/views/news.html',
    publicAccess: false,
    controller: 'VisitorsNewsCtrl'
})

So, if the controller will match I am sure that the syntax with $stateParams will work 因此,如果控制器可以匹配,我可以肯定$ stateParams的语法可以正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM