2017-05-10 119 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