javascript - 为什么我无法通过$stateParams在父子State之间传递参数?跟State之间的父子关系有关吗?
问题描述
路由设置(两个State之间有父子关系):
.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.父State的控制器中的代码:
.controller('MyProfileCtrl', function ($scope, $state) { $scope.goToMobileInput = function () { $state.go('tab.my-profile-mobileinput', {'mobile': '123456'}) };})
3.子State的控制器中的代码:
.controller('MobileInputCtrl', function ($scope, $stateParams) { alert($stateParams.mobile); // undefined})
能够跳转到子State,但在子State的控制器中无法接收到参数(访问参数时得到的结果是undefined,而非'123456')。看了网上资料这么写应该无误,是跟State之间的父子关系有关吗?
问题解答
回答1:你的router定义的有问题,params需要和url, views在同一级,views正如字面意思那样,是指定ui的,你在其中指定了params就有问题了,需要改成如下这样:
.state('tab.my-profile-mobileinput', { url: '/my/profile/mobileinput', params: {'mobile': null}, views: { 'tab-my': {templateUrl: 'templates/util-mobile-input.html',controller: 'MobileInputCtrl' } } })回答2:
$broadcast,我记得这种应该采用事件广播,可以将事件从父级作用域传播至子级作用域,你可以百度一下相关内容,应该能够解决
相关文章:
