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,我记得这种应该采用事件广播,可以将事件从父级作用域传播至子级作用域,你可以百度一下相关内容,应该能够解决
相关文章:
1. dockerfile - 为什么docker容器启动不了?2. html5 - node静态资源服务器设置了Cache-Control,但浏览器从来不走3043. 为什么我ping不通我的docker容器呢???4. debian - docker依赖的aufs-tools源码哪里可以找到啊?5. angular.js - angular内容过长展开收起效果6. docker - 各位电脑上有多少个容器啊?容器一多,自己都搞混了,咋办呢?7. docker内创建jenkins访问另一个容器下的服务器问题8. 对html实现监测 发现不对9. angular.js使用$resource服务把数据存入mongodb的问题。10. docker容器呢SSH为什么连不通呢?

网公网安备