0
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { RestfulComponent } from '/app/rest/app.restful.component.ts'; import { HttpModule } from '@angular/http'; @NgModule({ imports: [ BrowserModule, HttpModule ], declarations: [ RestfulComponent ], bootstrap: [ RestfulComponent ] }) export class AppModule { } 

app.restful.module.ts

import { Component } from '@angular/core'; import { RestfulService } from '/app/rest/restful/restfull.service.ts'; import {Http} from '@angular/http'; import { Observable } from 'rxjs'; @Component({ selector: 'my-app', template: ` <button (click)="onTestGet()">GET TEST</button><br> <p>Output: {{getData}}</p> `, providers: [RestfulService] }) export class RestfulComponent { getData: string; postData: string; // constructor (private _http: Http){} onTestGet(){ this._restfulService.getCurrentTime().subscribe( data => this.getData = JSON.stringify(data), error => alert(error), () => console.log("end") ); } } 

app.restful.component.ts

import {Injectable} from '@angular/core'; import {Http} from '@angular/http'; import {Headers} from '@angular/http'; import 'rxjs/add/operator/map' @Injectable() export class RestfulService { constructor (private _http: Http){} getCurrentTime(){ return this._http.get('http://date.jsontest.com').map(res => res.json()); } } 

restful.service.ts

enter image description here

[enter image description heretest.html2

I do not have much knowledge about angular. If in case you are missing any information or file, I am here to add.

I'll put the changed codes where they generated the following error:

What should I do??

0

1 Answer 1

2

declare RestfulService inside the constructor method :

constructor(private _restfulService: RestfulService) { } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.