首页 文章

在subscribe函数中设置变量时,更新Angular6中的视图

提问于
浏览
0

呈现文件的第一个组件选择html
enter image description here

当我点击浏览按钮时,我重定向到另一个组件Fileselect组件,这是第二个组件 this.goto('fileselect');

enter image description here

问题是当我从文件组件中选择一个文件时,我必须在第一个组件的输入字段中显示它

当我点击Select class按钮时,我正在调用redirect()函数 .

在文件选择组件中,我使用服务将文件名传递给第一个组件,如下面的代码所示

redirect(){
  this._messageService.filter(this.selectedFile);
  this.router.navigateByUrl('');

}

在我想要这个文件名的第一个组件中,我已经订阅了如图所示的功能

ngOnInit() {
   this._messageService.listen().subscribe((m:any) => {
            this.fileName = m
            this.loadXMLasJSON()
            console.log(this.fileName)

});}

当我在console.log中获取文件名时,我在订阅函数中分配了this.fileName变量 . 但是视图没有得到更新 .

我的观点代码

<input type="text"  class="form-control image-preview-filename" value="{{fileName}}" />

可能是什么问题呢?

请帮我解决一下这个 .

2 回答

  • -2

    请从 value 属性中删除 double quotes .

    Change 它到:

    <input type="file" class="form-control image-preview-filename" value={{fileName}} />

  • 0

    使用 [ngModel] 而不是 value

    <input type="text"  class="form-control image-preview-filename" [ngModel]="fileName" />
    

相关问题