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. mysql 查詢身份證號字段值有效的數據2. python bottle跑起來以后,定時執行的任務為什么每次都重復(多)執行一次?3. 視頻文件不能播放,怎么辦?4. html5 - HTML代碼中的文字亂碼是怎么回事?5. python - 爬蟲模擬登錄后,爬取csdn后臺文章列表遇到的問題6. visual-studio - Python OpenCV: 奇怪的自動補全問題7. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處8. javascript - 彈出一個子窗口,操作之后關閉,主窗口會得到相應的響應,例如網站的某些登錄界面,django后臺的管理等,這是怎么實現的呢?9. javascript - ios返回不執行js怎么解決?10. android - 分享到微信,如何快速轉換成字節數組
