Please review the previous article httpclient configuration before you proceed.
Use
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
1Import {Component, OnInit} from ' @angular/core ';2Import {HttpClient} from ' @angular/common/http ';3Import {Patient} from './app.patient.server ';4 @Component ({5Selector: ' App-root ',6Templateurl: './app.component.html ',7Styleurls: ['./app.component.css ']8 })9 Ten Export Class AppComponent implements OnInit { Onetitle = ' angular4.3 '; A results:string[]; -Mypatientlist:patient[] = []; - the - Constructor ( - Private Myhttp:httpclient - ) { } + -Ngoninit ():void { + This. Myhttp.get (' http://192.168.2.139:9002/api/patients ') A. Subscribe (data = { at This. Mypatientlist = (<any>data). Map (U =NewPatient ({ - Id:u.patientid, - Firstname:u.details.firstname, - Lastname:u.details.lastname, - Middlename:u.details.middlename, - Birthdate:u.details.birthdate, in Gender:u.details.gender, - Phonenumber:u.personalinfo.phonenumberprimary, to Zipcode:u.personalinfo.zipcode, + city:u.personalinfo.city, - Street:u.personalinfo.street, the Emailaddress:u.personalinfo.emailaddressprimary, * Citizenservicenumber:u.personalinfo.servicenumber $ }));
Panax Notoginseng - }); the } +}
(<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;Ten city:string; One street:string; A emailaddress:string; - citizenservicenumber:string; - the Public Constructor ( -Fields?: { - id:string, - firstname:string, + lastname:string, - middlename:string, + birthdate:string, A gender:string, at phonenumber:string, - zipcode:string, - city:string, - street:string, - emailaddress:string, - citizenservicenumber:string in }) { - //tslint:disable-next-line:curly to if(Fields) Object.assign ( This, fields); + } - the getfullname (): string { * return This. FirstName + "+ This. LastName; $ }Panax Notoginseng}
The foreground call is simple, and reading the entity class directly is fine.
1 <Divstyle= "Text-align:center">2 <H1>3 Welcome to {title}}!4 </H1>5 {{results}}6 <H2>Ngfor</H2>7 <ul>8 <Li*ngfor= "Let mypatient of Mypatientlist" >9 Id:{{mypatient.id}} FirstName: {{mypatient.firstname}}Ten </Li> One </ul> A </Div>
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.
Angular HttpClient Get method get Data