ANGULARJS Transformation Response Content _angularjs

Source: Internet
Author: User

The response content obtained from the remote API, usually in JSON format, sometimes needs to be converted to the retrieved content, such as removing some unwanted fields, alias The fields, and so on.

This article will experience how to achieve in Angualrjs.

In the main page, or from the controller to get the data.

<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>

Above, UserName, URL fields are converted from the source data, possibly userName the corresponding source data in the FullName, there are more fields in the energy data.

In Angularjs, it is a good practice to sort out the relationships between the module, such as by combing the following ways:

Angular.module (' publicapi.controllers ', []);
Angular.module (' publicapi.services ', []);
Angular.module (' publicapi.transformers ', []);
Angular.module (' Publicapi ', [
' publicapi.controllers ', '
publicapi.services ',
' Publicapi.transformers '

The data is still coming from the controller:

Angular.module (' publicapi.controllers ')
. Controller (' controllers. View ', [' $scope ', ' service. API ', function ($scope, API) {
$scope. Repos = Api.getuserrepos ("");

Controller depends on the service. API for this service.

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;
}
;

The Transformresponse field in the $http service is used to transform the data source. Services. The API relies on the Services.transformer.ApiResponse service, where the Service force completes the transformation of the data source.

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;

Above, a map transformation of the data source is made using the underscore.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.