2017-09-13 25 views
0

我讀過很多帖子,但都沒有工作: 我想通過一些數據使用$ state.go

這是我的狀態:

.state('app.404', { 
    url: '404', 
    views: { 
     '[email protected]': { 
     templateUrl: 'views/empty.html' 
     }, 
     '[email protected]': { 
     templateUrl: '404.html', 
     controller: 'fofController' 
     }, 
     '[email protected]': { 
     templateUrl: 'views/empty.html' 
     }, 
     params: {errorInfo: null} 
    } 
    }) 

我這是怎麼傳遞的數據:

$state.go('app.404', {errorInfo: {status: 403, message: 'unauthorised', fix: 'You have to login'}}); 

,這是我試圖抓住在我的控制器中的數據:

.controller('fofController', ['$scope', '$state', 
function ($scope, $state) { 
    console.log($state.params); 
}]) 

但答案是空的對象({})。我還用$ stateParams像下面,但我得到了相同的結果,一個空對象:

.controller('fofController', ['$scope', '$stateParams', 
function ($scope, $stateParams) { 
    console.log($stateParams); 
}]) 
+0

$ state.params。 errorInfo這是什麼給你 –

+0

答案是「undefined」 –

+0

你的'params'配置在你的狀態配置中的'views'配置中。將其升高一級。 –

回答

0

選項-1

.state('404.contact/', { 
    url: '/contact/:obj', 
    templateUrl: "contact.html", 
    controller: "contactCtrl", 
    params: { 
     obj: null, 
    } 
}) 
.state('404.header', { 
    url: '/header/:number', 
    templateUrl: "header.html", 
    controller: "headerCtrl", 
    params: { 
     obj: null 
    } 

})

選項-2

$state.go(..... 

您應該試試這個代碼而不是

$state.transitionTo(... 

選項-3

也許嘗試與URL

$stateProvider 
.state('user', { 
    url: '/user/:obj', 
    templateUrl: 'templates/user.html', 
    controller: 'UserCont' 
}) 

轉移也許這些都是解決問題的辦法。

Working example with $state.go()

+0

非常感謝你的回答,但這個問題本身就是一個愚蠢的錯誤。參數不應該在視圖部分。正如@JC Ford提到的:\t \t 你的參數配置在你的狀態配置中的視圖配置中。將其升高一級 –