javascript - 為什么我無法通過$stateParams在父子State之間傳遞參數(shù)?跟State之間的父子關(guān)系有關(guān)嗎?
問題描述
路由設(shè)置(兩個(gè)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ù)時(shí)得到的結(jié)果是undefined,而非'123456')。看了網(wǎng)上資料這么寫應(yīng)該無誤,是跟State之間的父子關(guān)系有關(guān)嗎?
問題解答
回答1:你的router定義的有問題,params需要和url, views在同一級(jí),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)該采用事件廣播,可以將事件從父級(jí)作用域傳播至子級(jí)作用域,你可以百度一下相關(guān)內(nèi)容,應(yīng)該能夠解決
相關(guān)文章:
1. node.js - mysql如何通過knex查詢今天和七天內(nèi)的匯總數(shù)據(jù)2. mysql 插入數(shù)值到特定的列一直失敗3. 360瀏覽器與IE瀏覽器有何區(qū)別???4. mysql - 百萬行的表中是否盡量避免使用update等sql語句?5. python - 在使用Pycharm時(shí)經(jīng)常看到如下的樣式,小括號(hào)里紅色的部分是什么意思呢?6. Python從URL中提取域名7. javascript - 新浪微博網(wǎng)頁版的字?jǐn)?shù)限制是怎么做的8. 怎么在網(wǎng)頁中設(shè)置圖片進(jìn)行左右滑動(dòng)9. javascript - 豆瓣的這個(gè)自適應(yīng)是怎么做的?10. javascript - 用jsonp抓取qq音樂總是說回調(diào)函數(shù)沒有定義
