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. css3 - 没明白盒子的height随width的变化这段css是怎样实现的?2. windows-7 - Win7中Vmware Workstatoin与Xampp中Apache服务器端口冲突?3. javascript - 为什么输入框什么都没输取到的值却是undefined??4. javascript - html标签的class属性里面怎么嵌入变量5. mysql新建字段时 timestamp NOT NULL DEFAULT ’0000-00-00 00:00:00’ 报错6. css - autoprefixer没有添加web-kit前缀7. javascript - JS 里面的 delete object.key 到底删除了什么?8. java - freemarker如何实现这种贼简单的效果9. android-studio - android app微信登录一定要申请了登录权限才能测试?10. javascript - 有什么兼容性比较好的办法来判断浏览器窗口的类型?

网公网安备