U
U
Urukhayy2016-12-29 13:30:07
Angular
Urukhayy, 2016-12-29 13:30:07

Who feeds into Http constructor or how to use providers?

Made a constructor accepting Http, but where to serve it? How to use providers? When I included just "Http" as the provider, and used the constructor with no arguments, the http object was available, but there was no effect.

import {
  Component,
  NgModule
} from '@angular/core';
import {
  Http
} from '@angular/http';
import 'rxjs/add/operator/map'
import {
  HttpModule
} from '@angular/http';
@Component({
  selector: 'my-app',
  providers: [HttpModule],
  template: `<h1>Test {{teste}}</h1>
  <table>

  </table>
  `
})
@NgModule({
  imports: [HttpModule]
})
export class AppComponent {
  teste: string;
  constructor(private http: Http) {
    this.teste = "ttttttttt";
    this.http.get('URL')
      .map(res => res.json())
      .subscribe(data => {
        this.teste = data;
      });
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2016-12-29
@Urukhayy

Why do you have two decorators attached to your class at once?
NgModule and Component perform different tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question