【问题标题】:Angular 11 - I can't display a value from local storageAngular 11 - 我无法显示本地存储中的值
【发布时间】:2021-11-22 05:56:59
【问题描述】:

我希望本地存储中的值显示在我的输入字段中。这是我的尝试,但它不起作用。

ts

 this.additionalID = parseInt(localStorage.getItem('some-key'));
  this.formValueWebinar.controls['user_id'].setValue(this.additionalID);

html

 <mat-form-field class="webinarTitleInput " appearance="outline">
               <mat-label class="font">Enter User ID</mat-label>
               <input type="text" matInput placeholder="User ID" formControlName='user_id' [innerHtml]= 'this.additionalID'>
            </mat-form-field>

【问题讨论】:

  • 你试过调试this.additionalID 吗?可能是nullundefined
  • 我检查了它,它有一个值
  • 您能否确认setValue 是在表单初始化之前还是之后执行的?您可以尝试在应用程序的 AfterViewInit 生命周期挂钩中运行 this.formValueWebinar.controls['user_id'].setValue(this.additionalID) 吗?
  • 你能分享更多的数据,比如this.formValueWebinar的结构以及你如何在模板中绑定this.formValueWebinar

标签: angular typescript local-storage


【解决方案1】:

用补丁值试试

this.formValueWebinar.controls['user_id'].patchValue(this.additionalID);

有时 setValues 函数不会更新它只是在代码上设置的值,也不会更新尝试使用补丁

【讨论】:

  • 还是没有显示
猜你喜欢
  • 2021-09-29
  • 1970-01-01
  • 2017-11-03
  • 1970-01-01
  • 2014-03-25
  • 2015-07-02
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多