AngularJS - Passing data between pages
著作權歸作者所有。
商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
作者:Ye Huang
鏈接:https://www.zhihu.com/question/33565135/answer/69651500
來源:知乎
1. 基於ui-router的頁面跳轉傳參
(1) 在AngularJS的app.js中用ui-router定義路由,比如現在有兩個頁面,一個頁面(producers.html)放置了多個producers,點擊其中一個目標,頁面跳轉到對應的producer頁,同時將producerId這個參數傳過去。
(2) 在producers.html中,定義點擊事件,比如ng-click="toProducer(producerId)",在ProducersCtrl中,定義頁面跳轉函數 (使用ui-router的$state.go接口):
(3) 在ProducerCtrl中,通過ui-router的$stateParams獲取參數producerId,譬如:
2. 基於factory的頁面跳轉傳參
舉例:你有N個頁面,每個頁面都需要用戶填選信息,最終引導用戶至尾頁提交,同時后一個頁面要顯示前面所有頁面填寫的信息。這個時候用factory傳參是比較合理的選擇(下面的代碼是一個簡化版,根據需求可以不同定制):
3. 基於factory和$rootScope.$broadcast()的傳參
(1) 舉例:在一個單頁中定義了nested views,你希望讓所有子作用域都監聽到某個參數的變化,並且作出相應動作。比如一個地圖應用,某個$state中定義元素input,輸入地址后,地圖要定位,同時另一個狀態下的列表要顯示出該位置周邊商鋪的信息,此時多個$scope都在監聽地址變化。
PS: $rootScope.$broadcast()可以非常方便的設置全局事件,並讓所有子作用域都監聽到。
(2) 在獲取地址的controller中:
(3) 在監聽地址變化的controller中:
4. 基於localStorage或sessionStorage的頁面跳轉傳參
注意事項:通過LS或SS傳參,一定要監聽變量,否則參數改變時,獲取變量的一端不會更新。AngularJS有一些現成的WebStorage dependency可以使用,譬如 gsklee/ngStorage · GitHub, grevory/angular-local-storage · GitHub。下面使用ngStorage來簡述傳參過程:
(1) 上傳參數到localStorage - Controller A
(2) 監聽localStorage中的參數變化 - Controller B
商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
作者:Ye Huang
鏈接:https://www.zhihu.com/question/33565135/answer/69651500
來源:知乎
1. 基於ui-router的頁面跳轉傳參
(1) 在AngularJS的app.js中用ui-router定義路由,比如現在有兩個頁面,一個頁面(producers.html)放置了多個producers,點擊其中一個目標,頁面跳轉到對應的producer頁,同時將producerId這個參數傳過去。
.state('producers', { url: '/producers', templateUrl: 'views/producers.html', controller: 'ProducersCtrl' }) .state('producer', { url: '/producer/:producerId', templateUrl: 'views/producer.html', controller: 'ProducerCtrl' })
(2) 在producers.html中,定義點擊事件,比如ng-click="toProducer(producerId)",在ProducersCtrl中,定義頁面跳轉函數 (使用ui-router的$state.go接口):
.controller('ProducersCtrl', function ($scope, $state) { $scope.toProducer = function (producerId) { $state.go('producer', {producerId: producerId}); }; });
(3) 在ProducerCtrl中,通過ui-router的$stateParams獲取參數producerId,譬如:
.controller('ProducerCtrl', function ($scope, $state, $stateParams) { var producerId = $stateParams.producerId; });
2. 基於factory的頁面跳轉傳參
舉例:你有N個頁面,每個頁面都需要用戶填選信息,最終引導用戶至尾頁提交,同時后一個頁面要顯示前面所有頁面填寫的信息。這個時候用factory傳參是比較合理的選擇(下面的代碼是一個簡化版,根據需求可以不同定制):
.factory('myFactory', function () { //定義factory返回對象 var myServices = {}; //定義參數對象 var myObject = {}; /** * 定義傳遞數據的set函數 * @param {type} xxx * @returns {*} * @private */ var _set = function (data) { myObject = data; }; /** * 定義獲取數據的get函數 * @param {type} xxx * @returns {*} * @private */ var _get = function () { return myObject; }; // Public APIs myServices.set = _set; myServices.get = _get; // 在controller中通過調set()和get()方法可實現提交或獲取參數的功能 return myServices; });
3. 基於factory和$rootScope.$broadcast()的傳參
(1) 舉例:在一個單頁中定義了nested views,你希望讓所有子作用域都監聽到某個參數的變化,並且作出相應動作。比如一個地圖應用,某個$state中定義元素input,輸入地址后,地圖要定位,同時另一個狀態下的列表要顯示出該位置周邊商鋪的信息,此時多個$scope都在監聽地址變化。
PS: $rootScope.$broadcast()可以非常方便的設置全局事件,並讓所有子作用域都監聽到。
.factory('addressFactory', ['$rootScope', function ($rootScope) { // 定義所要返回的地址對象 var address = {}; // 定義components數組,數組包括街道,城市,國家等 address.components = []; // 定義更新地址函數,通過$rootScope.$broadcast()設置全局事件'AddressUpdated' // 所有子作用域都能監聽到該事件 address.updateAddress = function (value) { this.components = value.slice(); $rootScope.$broadcast('AddressUpdated'); }; // 返回地址對象 return address; }]);
(2) 在獲取地址的controller中:
// 動態獲取地址,接口方法省略 var component = { addressLongName: xxxx, addressShortName: xxxx, cityLongName: xxxx, cityShortName: xxxx }; // 定義地址數組 $scope.components = []; $scope.$watch('components', function () { // 將component對象推入$scope.components數組 components.push(component); // 更新addressFactory中的components addressFactory.updateAddress(components); });
(3) 在監聽地址變化的controller中:
// 通過addressFactory中定義的全局事件'AddressUpdated'監聽地址變化 $scope.$on('AddressUpdated', function () { // 監聽地址變化並獲取相應數據 var street = address.components[0].addressLongName; var city = address.components[0].cityLongName; // 通過獲取的地址數據可以做相關操作,譬如獲取該地址周邊的商鋪,下面代碼為本人虛構 shopFactory.getShops(street, city).then(function (data) { if(data.status === 200){ $scope.shops = data.shops; }else{ $log.error('對不起,獲取該位置周邊商鋪數據出錯: ', data); } }); });
4. 基於localStorage或sessionStorage的頁面跳轉傳參
注意事項:通過LS或SS傳參,一定要監聽變量,否則參數改變時,獲取變量的一端不會更新。AngularJS有一些現成的WebStorage dependency可以使用,譬如 gsklee/ngStorage · GitHub, grevory/angular-local-storage · GitHub。下面使用ngStorage來簡述傳參過程:
(1) 上傳參數到localStorage - Controller A
// 定義並初始化localStorage中的counter屬性 $scope.$storage = $localStorage.$default({ counter: 0 }); // 假設某個factory(此例暫且命名為counterFactory)中的updateCounter()方法 // 可以用於更新參數counter counterFactory.updateCounter().then(function (data) { // 將新的counter值上傳到localStorage中 $scope.$storage.counter = data.counter; });
(2) 監聽localStorage中的參數變化 - Controller B
$scope.counter = $localStorage.counter; $scope.$watch('counter', function(newVal, oldVal) { // 監聽變化,並獲取參數的最新值 $log.log('newVal: ', newVal); });