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. 如何解決docker宿主機無法訪問容器中的服務?2. javascript - 如何使用nodejs 將.html 文件轉化成canvas3. angular.js - 輸入郵箱地址之后, 如何使其自動在末尾添加分號?4. javascript - html5的data屬性怎么指定一個function函數呢?5. 在mac下出現了兩個docker環境6. python - Scrapy存在內存泄漏的問題。7. java如何生成token?8. docker-compose中volumes的問題9. javascript - 后臺管理系統左側折疊導航欄數據較多,怎么樣直接通過搜索去定位到具體某一個菜單項位置,并展開當前菜單10. javascript - Web微信聊天輸入框解決方案
