AngularJS转换响应内容
作者:bea
从远程API获取到的响应内容,通常是json格式的,有时候需要对获取到的内容进行转换,比如去除某些不需要的字段,给字段取别名,等等。 本篇就来体验在AngualrJS中如何实现。 在主页面,还是从controller中拿数据。 <body ng-app="publicapi"><ul ng-controller="controllers.View"><li ng-repeat="repo in repos"><b ng-bind
从远程API获取到的响应内容,通常是json格式的,有时候需要对获取到的内容进行转换,比如去除某些不需要的字段,给字段取别名,等等。
本篇就来体验在AngualrJS中如何实现。
在主页面,还是从controller中拿数据。
<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字段是从源数据中转换而来的,可能userName对应源数据中的fullName,可能源数据中有更多的字段。
在AngularJS中,把module之间的关系梳理清楚是一种很好的习惯,比如按如下方式梳理:
angular.module('publicapi.controllers',[]);
angular.module('publicapi.services',[]);
angular.module('publicapi.transformers',[]);
angular.module('publicapi',[
'publicapi.controllers',
'publicapi.services',
'publicapi.transformers'
])
数据还是从controller中来:
angular.module('publicapi.controllers')
.controller('controllers.View',['$scope', 'service.Api', function($scope, api){
$scope.repos = api.getUserRepos("");
}]);
controller依赖于service.Api这个服务。
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服务中的transformResponse字段就是用来转换数据源的。services.Api依赖于services.transformer.ApiResponse这个服务,在这个服务力完成对数据源的转换。
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对数据源进行map转换。
有用 | 无用
本篇就来体验在AngualrJS中如何实现。
在主页面,还是从controller中拿数据。
<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字段是从源数据中转换而来的,可能userName对应源数据中的fullName,可能源数据中有更多的字段。
在AngularJS中,把module之间的关系梳理清楚是一种很好的习惯,比如按如下方式梳理:
angular.module('publicapi.controllers',[]);
angular.module('publicapi.services',[]);
angular.module('publicapi.transformers',[]);
angular.module('publicapi',[
'publicapi.controllers',
'publicapi.services',
'publicapi.transformers'
])
数据还是从controller中来:
angular.module('publicapi.controllers')
.controller('controllers.View',['$scope', 'service.Api', function($scope, api){
$scope.repos = api.getUserRepos("");
}]);
controller依赖于service.Api这个服务。
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服务中的transformResponse字段就是用来转换数据源的。services.Api依赖于services.transformer.ApiResponse这个服务,在这个服务力完成对数据源的转换。
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对数据源进行map转换。
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- javascript实现随机显示星星特效
- 基于javascript实现全国省市二级联动下拉选择菜单
- JS实现动态生成表格并提交表格数据向后端
- jQuery+css实现的时钟效果(兼容各浏览器)
- jQuery实现的分子运动小球碰撞效果
- jQuery+css3实现转动的正方形效果(附demo源码下载)
- 不用一句js代码初始化组件
- jQuery实现可以控制图片旋转角度效果(附demo源码下载)
- 封装属于自己的JS组件
- js+css绘制颜色动态变化的圈中圈效果
- Bootstrap树形控件使用方法详解
- window.onerror()的用法与实例分析
- 用window.onerror捕获并上报Js错误的方法
- jquery实现可旋转可拖拽的文字效果代码
- jquery+css3实现会动的小圆圈效果
- 再谈JavaScript异步编程
- 简单介绍jsonp 使用小结
- 理解javascript异步编程
- js实现的鼠标滚轮滚动切换页面效果(类似360默认页面滚动切换效果)