亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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ì)的介紹和示例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。

前言

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

注:參考文章Sharing Data Between Angular Controllers

一、service

可以寫(xiě)一個(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ù),用途也比較廣泛,使用場(chǎng)景比較多

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

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

others/localStorage

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

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

相關(guān)文章

最新評(píng)論