上次轉發過關於angularjs回退的文章,回退用到的還是js的回退功能,直接用history.back();實現功能,當時順便提了下$state.go()有關路由跳轉。 那這回就全面解析下$state.go();的功能.
下面是簡寫的步驟:
1.在目標頁面規定接受的參數:$stateProvider.state('page2', {params: {'data': null}});
2.傳參:$state.go('page2', {data: 'aaa'});
3.目標頁面接受參數:控制器注入$stateParams之后可以通過$stateParams.data來獲取‘aaa’;
1. ui-sref、$state.go 的區別
ui-sref
一般使用在 <a>...</a>;
<a ui-sref="message-list">消息中心</a>
$state.go('someState')
一般使用在 controller里面;
.controller('firstCtrl', function($scope, $state) { $state.go('login'); });
這兩個本質上是一樣的東西,我們看ui-sref的源碼:
...
element.bind("click", function(e) { var button = e.which || e.button; if ( !(button > 1 || e.ctrlKey || e.metaKey || e.shiftKey || element.attr('target')) ) { var transition = $timeout(function() { // HERE we call $state.go inside of ui-sref $state.go(ref.state, params, options); });
ui-sref最后調用的還是$state.go()方法
2. 如何傳遞參數
首先,要在目標頁面定義接受的參數:
傳參,ui-sref:
$state.go:
接收參數,
在目標頁面的controller里注入$stateParams,然后 "$stateParams.參數名" 獲取
文章轉自:http://www.cnblogs.com/jager/p/5293225.html