AngularJS轉(zhuǎn)換響應(yīng)內(nèi)容
從遠(yuǎn)程API獲取到的響應(yīng)內(nèi)容,通常是json格式的,有時(shí)候需要對(duì)獲取到的內(nèi)容進(jìn)行轉(zhuǎn)換,比如去除某些不需要的字段,給字段取別名,等等。
本篇就來(lái)體驗(yàn)在AngualrJS中如何實(shí)現(xiàn)。
在主頁(yè)面,還是從controller中拿數(shù)據(jù)。
<body ng-app="publicapi"> <ul ng-controller="controllers.View"> <li ng-repeat="repo in repos"> <b ng-bind="repo.userName"></b> <span ng-bind="repo.url"></span> </li> </ul> </body>
以上,userName, url字段是從源數(shù)據(jù)中轉(zhuǎn)換而來(lái)的,可能userName對(duì)應(yīng)源數(shù)據(jù)中的fullName,可能源數(shù)據(jù)中有更多的字段。
在AngularJS中,把module之間的關(guān)系梳理清楚是一種很好的習(xí)慣,比如按如下方式梳理:
angular.module('publicapi.controllers',[]);
angular.module('publicapi.services',[]);
angular.module('publicapi.transformers',[]);
angular.module('publicapi',[
'publicapi.controllers',
'publicapi.services',
'publicapi.transformers'
])
數(shù)據(jù)還是從controller中來(lái):
angular.module('publicapi.controllers')
.controller('controllers.View',['$scope', 'service.Api', function($scope, api){
$scope.repos = api.getUserRepos("");
}]);
controller依賴于service.Api這個(gè)服務(wù)。
angular.module('publicapi.services').factory('services.Api',['$q', '$http', 'services.transformer.ApiResponse', function($q, $http, apiResponseTransformer){
return {
getUserRepos: function(login){
var deferred = $q.defer();
$http({
method: "GET",
url: "" + login + "/repos",
transformResponse: apiResponseTransformer
})
.success(function(data){
deferred.resolve(data);
})
return deferred.promise;
}
};
}])
而$http服務(wù)中的transformResponse字段就是用來(lái)轉(zhuǎn)換數(shù)據(jù)源的。services.Api依賴于services.transformer.ApiResponse這個(gè)服務(wù),在這個(gè)服務(wù)力完成對(duì)數(shù)據(jù)源的轉(zhuǎn)換。
angular.module('publicapi.transformers').factory('services.transformer.ApiResponse', function(){
return function(data){
data = JSON.parse(data);
if(data.length){
data = _.map(data, function(repo){
return {userName: reop.full_name, url: git_url};
})
}
return data;
};
});
以上,使用了underscore對(duì)數(shù)據(jù)源進(jìn)行map轉(zhuǎn)換。
- AngularJS入門教程之 XMLHttpRequest實(shí)例講解
- AngularJS出現(xiàn)$http異步后臺(tái)無(wú)法獲取請(qǐng)求參數(shù)問(wèn)題的解決方法
- AngularJS通過(guò)$http和服務(wù)器通信詳解
- AngularJS中$http服務(wù)常用的應(yīng)用及參數(shù)
- Angularjs中$http以post請(qǐng)求通過(guò)消息體傳遞參數(shù)的實(shí)現(xiàn)方法
- 后端接收不到AngularJs中$http.post發(fā)送的數(shù)據(jù)原因分析及解決辦法
- 簡(jiǎn)介AngularJS中$http服務(wù)的用法
- 詳解AngularJS中$http緩存以及處理多個(gè)$http請(qǐng)求的方法
- AngularJS中如何使用$http對(duì)MongoLab數(shù)據(jù)表進(jìn)行增刪改查
- 對(duì)比分析AngularJS中的$http.post與jQuery.post的區(qū)別
- AngularJS的ng Http Request與response格式轉(zhuǎn)換方法
相關(guān)文章
angular實(shí)現(xiàn)圖片懶加載實(shí)例代碼
本篇文章主要介紹了angular實(shí)現(xiàn)圖片懶加載實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Angular應(yīng)用Bootstrap過(guò)程步驟邏輯詳解
這篇文章主要為大家介紹了Angular應(yīng)用Bootstrap過(guò)程步驟邏輯詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
AngularJS基礎(chǔ) ng-keydown 指令簡(jiǎn)單示例
本文主要介紹AngularJS ng-keydown 指令,在這里幫大家整理了ng-keydown 指令的基礎(chǔ)資料,并附有代碼,有需要的朋友可以參考下2016-08-08
angularJs中datatable實(shí)現(xiàn)代碼
本篇文章主要介紹了angularJs中datatable實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
關(guān)于AngularJS中ng-repeat不更新視圖的解決方法
今天小編就為大家分享一篇關(guān)于AngularJS中ng-repeat不更新視圖的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Angular Module聲明和獲取重載實(shí)例代碼
這篇文章主要介紹了Angular Module聲明和獲取重載實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09
angularJs中$http獲取后臺(tái)數(shù)據(jù)的實(shí)例講解
今天小編就為大家分享一篇angularJs中$http獲取后臺(tái)數(shù)據(jù)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
angular2實(shí)現(xiàn)統(tǒng)一的http請(qǐng)求頭方法
今天小編就為大家分享一篇angular2實(shí)現(xiàn)統(tǒng)一的http請(qǐng)求頭方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
解決nodejs中使用http請(qǐng)求返回值為html時(shí)亂碼的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決nodejs中使用http請(qǐng)求返回值為html時(shí)亂碼的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02

