S
S
Skrolea2016-03-11 14:02:55
JavaScript
Skrolea, 2016-03-11 14:02:55

How to "stop" an API call?

Good afternoon.
I want to output variables to the template from the database. Made a service

import {Http} from 'angular2/http';
import {Injectable} from 'angular2/core';
import 'rxjs/Rx';

@Injectable()
export class MainService {
  public http:Http;
  public localhostUrl = 'http://localhost:3000/api/v1/main/text/';
  public pageTitleBase:Object = {};

constructor(http: Http) {
  this.http = http;
}
    getVar() {
     return this.http.get(this.localhostUrl)
          .map(res => res.json())
          .toPromise();
      }
}

and call it in the component
import {Component} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common';
import {MainService} from '../../shared/services/main.service';
import {Title} from 'angular2/platform/browser';
@Component({
  selector: 'sd-home',
  moduleId: module.id,
  viewProviders: [MainService],
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css'],
  directives: [CORE_DIRECTIVES]
})
export class HomeComponent {
  public pageTitle : String;
  private text_content : Object;
  constructor (private _title:Title, private _main:MainService) {}
ngOnInit() { this.getTextContent(); }

    getTextContent() {
    
    this._main.getVar()
    .subscribe(text_content =>
    this.text_content = text_content,
    error =>  this.errorMessage = <any>error);

   }
}

And there is a constant call to the API, non-stop. How to make the data "climb" once and calm down on it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question