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. linux - 编译安装mysql 5.6.232. angular.js - vue中类似于angular的ng-change的指令是?3. html5 - 在一个页面中 初始了两个swiper 不知道哪里错了 一直不对4. PHP能实现百度网盘的自动化么?5. android - 使用vue.js进行原生开发如何进行Class绑定6. node.js - 微信的自动回复问题7. node.js - vue服务端渲染如何部署到线上8. css3 - 请问一下在移动端CSS布局布局中通常需要用到哪些元素,属性?9. 网页爬虫 - python requests爬虫,如何post payload10. 如何把3个不同的MySQL数据库数据整合到一个MySQL数据库中?
