成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

javascript - 為什么我無法通過$stateParams在父子State之間傳遞參數(shù)?跟State之間的父子關(guān)系有關(guān)嗎?

瀏覽:177日期:2024-03-07 18:45:39

問題描述

路由設(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)該能夠解決

標(biāo)簽: JavaScript
相關(guān)文章: