【问题标题】:Populating textarea from DB / ng2从 DB / ng2 填充 textarea
【发布时间】:2017-10-08 22:23:18
【问题描述】:

我正在尝试使用我的 firebase 数据库中的数据填充文本区域。我知道我应该使用 ngModel,但我比较拘泥于语法。

  <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));

  }

【问题讨论】:

    标签: angular typescript httprequest get-request


    【解决方案1】:

    当您获取数据时,将其存储在一个变量中,然后您可以在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>
    

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 2012-03-14
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多