首页 > 解决方案 > Angular 11 - 我无法显示本地存储中的值

问题描述

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

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>

标签: angulartypescriptlocal-storage

解决方案


尝试使用补丁值

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

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


推荐阅读