search

Home  >  Q&A  >  body text

Why can't I pass parameters between parent and child State via $stateParams? Is it related to the father-son relationship between State?

  1. Routing settings (there is a parent-child relationship between the two States):

.state("tab.my-profile", {
  url: "/my/profile",
  views: {
    "tab-my": {
      templateUrl: "templates/tab-my-profile.html",
      controller: "MyProfileCtrl"
    }
  }
})
  .state("tab.my-profile-mobileinput", {
    url: "/my/profile/mobileinput",
    views: {
      "tab-my": {
        params: {"mobile": null}
        templateUrl: "templates/util-mobile-input.html",
        controller: "MobileInputCtrl",
      }
    }
  })

2. Code in the controller of the parent State:

.controller("MyProfileCtrl", function ($scope, $state) {
  $scope.goToMobileInput = function () {
    $state.go("tab.my-profile-mobileinput", {"mobile": "123456"})
  };
})

3. Code in the controller of the sub-State:

.controller("MobileInputCtrl", function ($scope, $stateParams) {
  alert($stateParams.mobile);  // undefined
})

Can jump to the sub-State, but cannot receive parameters in the controller of the sub-State (the result obtained when accessing the parameters is undefined, not "123456"). After reading the information on the Internet, this writing should be correct. Is it related to the father-son relationship between State?

淡淡烟草味淡淡烟草味2776 days ago963

reply all(2)I'll reply

  • 黄舟

    黄舟2017-05-27 17:46:33

    There is something wrong with your router definition. Params need to be at the same level as url and views. Views, as the name suggests, specify ui. If you specify params in it, there is a problem. You need to change it to the following:

      .state("tab.my-profile-mobileinput", {
        url: "/my/profile/mobileinput",
        params: {"mobile": null},
        views: {
          "tab-my": {
            templateUrl: "templates/util-mobile-input.html",
            controller: "MobileInputCtrl"
          }
        }
      })

    reply
    0
  • ringa_lee

    ringa_lee2017-05-27 17:46:33

    $broadcast, I remember this should use event broadcasting, which can propagate events from the parent scope to the child scope. You can search the relevant content on Baidu and it should be able to solve it

    reply
    0
  • Cancelreply