arrays - HTTP GET requests data endlessly -


i can't figure out why code requests data endlessly. in service data array of objects. want http, map, subscribe in service, because need service contain local array, because need array in rest of webpage. using developer tool in browser method gets requested endlessly, until browser crashes.

worth noticing incoming data not "file.json", plain url. in backend use jackson objectmapper sending data array objects.

btw, when modify code service sending http.get('someurl') , doing map , subscribe in component, works fine. use delete data-methods, didn't work when modified mapping , subscription in component.

hope can see obvious errors!

the codes loop endlessly:

myservice:

export class myservice {      private dummys: array<dummy>;      constructor(private http: http) {         this.dummys = new array<dummy>();     }     getdata() {         this.http.get('someurl')             .map((res: response) => res.json())             .subscribe(dummys => this.dummys = dummys);         return this.dummys;     }      savedata(dummy: dummy) {          let body = json.stringify(dummy);         let headers = new headers({ 'content-type': 'application/json' });         let options = new requestoptions({ headers: headers });          this.http.get('someurl', body, options)             .map((res: response) => res.json())             .subscribe(dummy => this.dummys.push(dummy));         return this.dummys;     }      deletedata() {          let index = this.dummys.indexof(dummy);          let body = json.stringify(dummy);         let headers = new headers({ 'content-type': 'application/json' });         let options = new requestoptions({ headers: headers });          this.http.post('someurl', body, options)             .map((res: response) => res.json())             .subscribe(dummy => this.dummys = this.dummys.splice(index, 1);         return this.dummys;     } } 

mycomponent:

export class mycomponent {      name: string;     id: string;     date: string;     dummys: array<dummy>;      constructor(public _myservice: myservice, public _router: router) { }      getdatas() {         this._myservice.getdata()     }      savedatas() {         let dummy = new dummy(this.id, this.name, this.date)         this._myservice.savedata(dummy)         this.name = '';         this._myservice.getdata();     }      deletedatas(dummy) {         this._myservice.deletedata(dummy);         this._myservice.getdata();     }  } 

my template in mycomponent:

<form (submit)="savedatas()">     <input required [(ngmodel)]="name" placeholder="add data"> </form> <br> <table>     <tr *ngfor="let data of getdatas()">     <td>{{data.name}}</td>     <td> {{data.date}} </td>     <td><button (click)="removedatas(data)">remove</button></td>     </tr> </table> 

subscribe the observable in mycomponent , return observable in service:

export class myservice {  ...  getdata() {     return this.http.get('someurl')         .map((res: response) => res.json()) }  ... }  export class mycomponent {  ...  ngoninit() {    this.getdatas(); }  getdatas() {     this._myservice.getdata().subscribe(         response => { this.datas = response}); } ... } 

and in template:

<tr *ngfor="let data of datas"> 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -