2017-05-10 3 views
1

내 firebase 데이터베이스의 데이터로 텍스트 영역을 채우려고합니다. ngModel을 사용해야한다는 것을 알고 있지만 구문에 다소 차질이 있습니다.DB/ng2에서 텍스트 영역 채우기

<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

관련 문제