DIV CSS 佈局教程網

AngularJS 中 ui-sref 與 $state.go 如何傳遞參數
編輯:JavaScript基礎知識     

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.參數名」的方式取得對應的值。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved