Angular HttpClient Get method get Data

Source: Internet
Author: User
Tags export class

This.myhttp.get (' http://192.168.2.139:9002/api/patients ') method, read Webapi. Because the Get method reads the data through the Ajax method, the server can be accessed across domains, specifically querying the WEBAPI article
 1 Import {Component, OnInit} from ' @angular/core '; 2 Import {HttpClient} from ' @angular/common/http '; 3 Import {Patient} from './app.patient.server ';  4 @Component ({5 selector: ' App-root ', 6 templateurl: './app.component.html ', 7 styleurls: ['./app.component.css '] 8  }) 9 Export class AppComponent implements OnInit {One title = ' angular4.3 '; results:string[];13 mypatientlist: patient[] = [];14 Constructor (+ private myhttp:httpclient18) {}19 ngoninit (): void {this.my Http.get (' http://192.168.2.139:9002/api/patients '). Subscribe (data = {This.mypatientlist = (<any >data). Map (U = new Patient ({id:u.patientid,25 firstname:u.details.firstname,26 L           astname:u.details.lastname,27 middlename:u.details.middlename,28 birthdate:u.details.birthdate,29 gender:u.details.gender,30 phonenumber:u.personalinfo.phonenumberprimary,31 ZIPCode:u.personalinfo.zipcode,32 city:u.personalinfo.city,33 street:u.personalinfo.street,34         Emailaddress:u.personalinfo.emailaddressprimary,35 citizenservicenumber:u.personalinfo.servicenumber36 }));
37 38}); 39}40}
(<any>data). The map () method directly transforms the data read into an entity class.
"Note: You need to see your own JSON file, corresponding to a good structure" many of the JSON on the web begins with results[. To be used (<any>data). Results.map () This problem really stuck to me for a long time, with the help of a colleague changqing to solve.

Entity class code, as follows:
1 Export class Patient {2     id:string; 3     firstname:string; 4     lastname:string; 5     middlename:string; 6
   birthdate:string; 7     gender:string; 8     phonenumber:string; 9     zipcode:string;10     city:string;11     street:string;12     emailaddress:string;13     Citizenservicenumber:string;14     (constructor)       : {         id:string,18         FirstName: string,19         lastname:string,20         middlename:string,21         birthdate:string,22         gender:string,23         phonenumber:string,24         zipcode:string,25         city:string,26         street:string,27         emailaddress:string,28         Citizenservicenumber:string29       }) {       tslint:disable-next-line:curly31       if (fields) Object.assign (     }33     getfullname (): string {       return this. FirstName + "+ this. lastname;36     }37   }

The foreground call is simple, and reading the entity class directly is fine.

1 <div style= "Text-align:center" > 2   

Display as:

Note: This usage is also very simple to write, but also need to pass parameters, header files and other methods, but already can basically read the data.

Transferred from: http://www.cnblogs.com/cxd1008/p/7685999.html

Angular HttpClient Get method get Data

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.