欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular.js跨controller實(shí)現(xiàn)參數(shù)傳遞的兩種方法

 更新時(shí)間:2017年02月20日 09:22:07   作者:xishiyi7  
這篇文章主要給大家介紹了關(guān)于Angular.js跨controller實(shí)現(xiàn)參數(shù)傳遞的兩種方法,文中給出了詳細(xì)的介紹和示例代碼,相信對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。

前言

由于controllers之間不共享scope,如果希望在controllers之間傳遞參數(shù),可能需要通過其他的方式實(shí)現(xiàn),以下是當(dāng)前我用到的兩種在controllers之間傳遞參數(shù)的方法。

注:參考文章Sharing Data Between Angular Controllers

一、service

可以寫一個(gè)包含get/set的service,取參數(shù)/賦參數(shù)

.factory('paramService',function(){
 return {
 result:[],
 getResult:function(){
 return this.result;
 },
 setResult:function(res){
 this.result = res;
 }
 };
})

然后可以在controllerOne中賦值,在controllerTwo中取值

// 賦值
.controller('one',function(paramService){
 paramService.setResult('one');
})

// 取值
.controller('two',function(paramService){
 var param = paramService.getResult();
})

二、$stateParams

第二種方法用于路由間傳遞參數(shù),用途也比較廣泛,使用場景比較多

// 傳參
.state('one',{
 url:'one',
 controller:'one',
 template:'one.html',
 params:{
 name:'john'
 }
})

// 取參
.controller('one',function($stateParams){
 var name = $stateParams.name;
})

others/localStorage

其他方法可以使用一些h5的小技巧,比如使用localStorage來存參/取參,其他的方法,暫時(shí)沒想到也沒用到,有待后續(xù)補(bǔ)充.

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用Angular能帶來一定的幫助,如果有疑問大家可以留言交流。

相關(guān)文章

最新評論