2017-05-10 120 views
1

我想從我的firebase數據庫中獲取textarea數據。我知道我應該使用ngModel,但我寧願停留在語法上。從DB/ng2填充textarea

<textarea rows="3" cols="35" [(ngModel)]=""> read from db.. </textarea> 

在空的「」,我試圖寫HttpService.getData();但那不起作用。 我也嘗試編寫http.service.ts.getData(),但也沒有工作。

通過使用下面的代碼,我能夠得到響應輸出到控制檯,所以我應該用這個......莫過於有人告訴我怎麼做?

@Injectable() 
export class HttpService { 

    constructor(private http: Http) { } 


    getData() { 
    return this.http.get('https://URL-HERE.json').map((response: Response) => response.json()); 
} 


} 

@Component({ 
    selector: 'message-form', 
    templateUrl: './message-form.component.html', 
    providers: [HttpService] 
}) 
    export class MessageFormComponent implements OnInit{ 
constructor(private httpService: HttpService) { } 

    ngOnInit(){ 

    this.httpService.getData() 
     .subscribe(
     (data: any) => console.log(data)); 

    } 

回答

0

當你得到的數據,將其存儲在一個變量,你就可以在ngModel使用:

data: any = {}; // replace any with the actual type 

ngOnInit() { 
    this.httpService.getData() 
    .subscribe(
     (data: any) => { 
     this.data = data; 
     console.log(data); 
    }); 
    } 

然後:

<textarea rows="3" cols="35" [(ngModel)]="data"> read from db.. </textarea> 
+0

啊,謝謝一堆!這就是訣竅:) –

+0

很高興聽到,你非常歡迎! :) – Alex