javascript - 為什么我無法通過$stateParams在父子State之間傳遞參數(shù)?跟State之間的父子關(guān)系有關(guān)嗎?
問題描述
路由設(shè)置(兩個State之間有父子關(guān)系):
.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})
能夠跳轉(zhuǎn)到子State,但在子State的控制器中無法接收到參數(shù)(訪問參數(shù)時得到的結(jié)果是undefined,而非'123456')。看了網(wǎng)上資料這么寫應(yīng)該無誤,是跟State之間的父子關(guān)系有關(guān)嗎?
問題解答
回答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,我記得這種應(yīng)該采用事件廣播,可以將事件從父級作用域傳播至子級作用域,你可以百度一下相關(guān)內(nèi)容,應(yīng)該能夠解決
相關(guān)文章:
1. list - python 求助2. linux - Ubuntu下編譯Vim8(+python)無數(shù)次編譯失敗3. python - TypeError: tryMsgcode() takes exactly 2 arguments (0 given)4. javascript - react,獲取radio的值出錯5. css - 移動端 line-height安卓錯位,蘋果機(jī)正常用,縮放解決了,可是又出來了占位的問題6. extra沒有加載出來7. javascript - 彈出一個子窗口,操作之后關(guān)閉,主窗口會得到相應(yīng)的響應(yīng),例如網(wǎng)站的某些登錄界面,django后臺的管理等,這是怎么實(shí)現(xiàn)的呢?8. 環(huán)境搭建 - anaconda 創(chuàng)建python2.7環(huán)境中打開編譯器確是3.6版本9. mysql replace 死鎖10. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時間會消失是什么情況?
