AngularJS路由 $state服務、路由事件、獲取路由參數

1 ui-sref、$state.go 的區別

ui-sref 一般使用在

<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.參數名” 獲取

這裏寫圖片描述

發佈了26 篇原創文章 · 獲贊 62 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章