2016-07-13 3 views
-1

에 대한 바이더를 얻을 실행할 때?나는 내가 오류를 다음 프로젝트를 HttpService를

내 AppComponent :

import {Component} from 'angular2/core'; 
import {HttpService} from "./htttp.service"; 

@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
    <div class="input"> 
    <label for="title">Title</label> 
    <input type="text" id="title" #title> 
    </div> 
    <div class="body"> 
    <label for="body">Body</label> 
    <input type="text" id="body" #body> 
    </div> 
    <div class="user-id"> 
    <label for="user-id">User ID</label> 
    <input type="text" id="user-id" #userid> 
    </div> 
    <button (click)="onPost(title.value, body.value, userid.value)">Post Data</button> 
    <button (click)="onGetPosts()">Get All Posts</button> 
    <p>Response: {{response | json}}</p> 
    </div>, 

    providers: [HttpService] 
     `, 
}) 
export class AppComponent { 
    response: string; 

    constructor(private _httpService: HttpService){} 
    onGetPosts(){ 
    this._httpService.getPosts().subscribe(
     response => this.response=response, 
     error => console.log(error) 
    ) 
    } 

} 

HttpService를 :

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

@Injectable() 

export class HttpService{ 
    constructor(private _http:Http){} 

    getPosts():Observable<any>{ 
    return this._http.get('http://jsonplaceholder.typicode.com/posts').map(res => res.json()); 
    } 

} 

및 boot.ts :

import {bootstrap} from 'angular2/platform/browser'; 
import {AppComponent} from "./app.component"; 
import {HTTP_PROVIDERS} from "angular2/http"; 

bootstrap(AppComponent, [HTTP_PROVIDERS]); 

문제가?

+0

당신의 문제는 여기에'수입 {HttpService를} "./htttp.service"에서, 당신은 htttp''3't'이'그것을해야 겨우 2 – AngJobs

+0

감사합니다! 어리석은 실수! :) –

+0

걱정할 필요가 없습니다. 이제 'UP'버튼을 누르십시오! 감사! – AngJobs

답변

1

import {HttpService} from "./http.service"; 
0

해야 당신은 당신의 공급자를 선언하기 전에 템플릿 문자 그대로 '다시 견적을'닫습니다 잊어 버렸습니다.

@Component({ 
    selector: 'my-app', 
    template: ` 
    <div> 
    <div class="input"> 
    <label for="title">Title</label> 
    <input type="text" id="title" #title> 
    </div> 
    <div class="body"> 
    <label for="body">Body</label> 
    <input type="text" id="body" #body> 
    </div> 
    <div class="user-id"> 
    <label for="user-id">User ID</label> 
    <input type="text" id="user-id" #userid> 
    </div> 
    <button (click)="onPost(title.value, body.value, userid.value)">Post Data</button> 
    <button (click)="onGetPosts()">Get All Posts</button> 
    <p>Response: {{response | json}}</p> 
    </div>`, 
    providers: [HttpService] 
}) 
관련 문제