Directly on the code bar, before the configuration directly view angular's previous articles.
Background TS Code:
1 Import {Component, OnInit} from ' @angular/core '; 2 Import {Patient} from './patients.service '; 3 Import {HttpClient, Httpparams, httpheaders} from ' @angular/common/http '; 4 5 @Component ({6 selector: ' Patient-component ', 7 templateurl: ' App/app-patient/patient.component.html ', 8 Providers: [Patientsservice] 9}) Export class Patientcomponent implements OnInit {One to ten results:string[] ; Mypatientlist:patient[] = []; Mypatient:patient; Mywebapiurl = ' http://localhost:52513/api/patients/'; Myhttphead = {Headers:new httpheaders ({' Content-type ': ' Application/json '})}; + Private myhttpparams = new Httpparams (). Set (' username ', ' dih '). Set (' Password ', ' dih '); Constructor (Myhttp:httpclient//private Mypatientsservice:patientsserv Ice ngoninit (): void {this.results = ["Ngoninit () method"]; This.getall () ; 27} 28 GetAll () {//console.log ("GetAll"); this.mypatientlist = []; This.myhttp.get (this.myweb Apiurl). Subscribe (data = {+ = Count = (<array<string>>data). length; 34 for (var i = 0; i < count; i++) {This.myPatientList.push (new Patient ({37 Id:data[i]. Patientid, Firstname:data[i]. Details.firstname, Lastname:data[i]. Details.lastname, Middlename:data[i]. Details.middlename, Birthdate:data[i]. Details.birthdate, Gender:data[i]. Details.gender, Phonenumber:data[i]. Personalinfo.phonenumberprimary, Zipcode:data[i]. Personalinfo.zipcode, City:data[i]. Personalinfo.city, Street:data[i]. Personalinfo.streeT, Emailaddress:data[i]. Personalinfo.emailaddressprimary, Citizenservicenumber:data[i]. Personalinfo.servicenumber (49})); 50} 51} 52); GetbyId (id:string) {This.myhttp.get (This.mywebapiurl + ID). Subscribe ( data = {this.mypatient = new Patient ({id:data[' Patientid '), 61 firstname:data[' Details '. FirstName, lastname:data[' Details '. LastName, middlename:data[' Details ']. MiddleName, birthdate:data[' Details ']. BirthDate, gender:data[' Details ']. Gender, phonenumber:data[' Personalinfo ']. Phonenumberprimary, zipcode:data[' Personalinfo ']. ZIPCODE, city:data[' Personalinfo '). City, 69 street:data[' Personalinfo '. Street, emailaddress:data[' Personalinfo '). Emailaddressprimary, citizenservicenumber:data[' Personalinfo ']. Servicenumber 72}); 73}); Httppostexample (firstname:string, lastname:string) {Const BODY = {80 "Details": {Bayi "FirstName": FirstName, "LastName": LastName, the "Mai Denname ":", "MiddleName": "," Customid ":", "BirthDate": "201 7-10-18t11:05:51.017 ", Gender": 1, "Anatomy": "Bo" Dyweight ":" Bodyheight ": 175," Leftfootlength ": 0," Rightfoot " Length ": 0, 94" stridelengthwalking ": 0," stridelengthrunning ": 0, 96" Pelviswidth ": 0, 97 "Leftupperleglength": 0, 98 "rightupperleglength": 0, "leftlowerleglength": 0,1 XX "Rightlowerleglength": 0101},102 "Personalinfo": {103 "Service Number ":", 104 "emailaddressprimary": "[email protected]", "Emailaddresssecondary" ":", 106 "phonenumberprimary": ", 107" Phonenumbersecondary ":", 108 "Str Eetaddress ":", 109 "ZIPCode": "," City ":", 111 "Street": ", 112 "Country": ", 113" emergencycontactdetails ":"},115 "ADDITIONALPR Operties ":"};117 this.myhttp.post (This.mywebapiurl, Body, This.myhttphead) 118. Subscribe (119 (val) = {Console.log (' POST call successful value returned in body ', Val); 121 This.getall (); 122},123 response = {124 Console.log (' POST call in error ', response); 125 },126 () = {127 Console.log (' The POST observable is now completed. '); 129}131 Httpputexample (id:string, firstname:string, lastname:string) {133 Const BODY = {134 "Patientid": id,135 "Details": {136 "FirstName": Firstnam e,137 "LastName": lastname,138 "Maidenname": ", 139" MiddleName ":", 140 "Customid": ", 141" BirthDate ":" 2017-10-18t11:05:51.017 ", 142" Gender ": 114 3},144 "Anatomy": {145 "bodyweight": 75,146 "bodyheight": 175,147 "Leftfootlength": 0,148 "rightfootlength": 0,149 "stridelengthwalking": 0 , "StridelengthrUnning ": 0,151" pelviswidth ": 0,152" leftupperleglength ": 0,153" Rightuppe Rleglength ": 0,154" leftlowerleglength ": 0,155" rightlowerleglength ": 0156},1 "Personalinfo": {158 "Servicenumber": ", 159" emailaddressprimary ":" [email protected] ", emailaddresssecondary": ", 161" phonenumberprimary ":", 162 "Phonenumbersecondary": ", 163" streetaddress ":", 164 "ZIPCode": ", 165 "City": ", 166" Street ":", 167 "Country": ", 168" Emergencycontac Tdetails ": ' 169},170" additionalproperties ":" 171};172 This.myhttp.put (this.my Webapiurl + ID, body, this.myhttphead) 173. Subscribe (174 (val) + = {175 console. Log (' Put call successful', Val); 176 this.getall (); 177},178 response = {179 Console.lo G (' Put call in error ', response);},181 () = {182 Console.log (' Put ' now C ompleted. '); 183}); 184 185}186 187 188 Delbyid (id:string) {189 This.myhttp.delete (This.mywebapiurl + ID) . Subscribe (RES) = {Console.log ("del OK", res); 191 this.getall (); 192}, (Err) => ; {193 Console.log ("Del err", err); 194}); 195 196}197}
Front Code:
1 <div>patient-component.html</div> 2 3 4 {{results}} 5 6 。。。。。。。。 angular4.3 httpclient end .....
Transferred from: http://www.cnblogs.com/cxd1008/p/7717037.html
Angular HttpClient Post put del method