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. ddos - apache日志很多其它网址,什么情况?2. 高并发写入和更新mysql3. javascript - 数组的过滤和渲染4. javascript - 这里为啥会报错?5. java - String s=new String()与String s = ""的区别6. javascript - 修改表单多选项时和后台同事配合的问题。7. MySQL 水平拆分之后,自动增长的ID有什么好的解决办法?8. mac里的docker如何命令行开启呢?9. java - springMVC配置的controller无法返回jsp文件10. html - 微信浏览器h5<video>标签问题
