ERROR: 无法将值“$event”分配给模板变量“element”。模板变量是只读的。

huangapple go评论76阅读模式
英文:

ERROR: Cannot assign value "$event" to template variable "element". Template variables are read only

问题

以下是翻译好的部分:

"Can someone please help me what is wrong with this code?

 <div *ngFor="let element of list; let index=index">
    <mat-form-field>
      <input matInput type="string" [(ngModel)]="element" name="element" #field="ngModel">
    </mat-form-field>
</div>

I am getting an error
ERROR: Cannot assign the value "$event" to template variable "element". Template variables are read-only

I tried below solution

<div *ngFor="let element of list; let index=index">
  <mat-form-field>
    <input matInput type="string" [(ngModel)]="list[index]" name="element" #field="ngModel">
  </mat-form-field>
</div>

This solution works but when I try to edit the input field it takes only one character at a time, for every character I need to click inside the input box and then type"

英文:

Can someone please help me what is wrong with this code?

 &lt;div *ngFor=&quot;let element of list; let index=index&quot;&gt;
    &lt;mat-form-field&gt;
      &lt;input matInput type=&quot;string&quot; [(ngModel)]=&quot;element&quot; name=&quot;element&quot; #field=&quot;ngModel&quot;&gt;
    &lt;/mat-form-field&gt;
&lt;/div&gt;

I am getting an error
ERROR: Cannot assign the value &quot;$event&quot; to template variable &quot;element&quot;. Template variables are read only

I tried below solution

&lt;div *ngFor=&quot;let element of list; let index=index&quot;&gt;
  &lt;mat-form-field&gt;
    &lt;input matInput type=&quot;string&quot; [(ngModel)]=&quot;list[index]&quot; name=&quot;element&quot; #field=&quot;ngModel&quot;&gt;
  &lt;/mat-form-field&gt;
&lt;/div&gt;

This solution works but when I try to edit the input field it takes only one character at a time, for every character I need to click inside the input box and then type

答案1

得分: 0

以下是翻译好的部分:

这可能不适用于最新版本。 您需要理解模板变量。 Angular-understanding template variables

将代码更改如下:

<div *ngFor="let element of list; let index=index">
  <mat-form-field>
    <input matInput type="string" [ngModel]="element" (change)="updateElement($event.target.value, index)" name="element" #field="ngModel">
  </mat-form-field>
</div>

在.ts文件中,编写更新函数如下:

updateElement(value: string, index: number) {
  this.list[index] = value;
}
英文:

This may not work with the latest version. You need to understand template variables. Angular-understanding template variables

Change the code like below

&lt;div *ngFor=&quot;let element of list; let index=index&quot;&gt;
&lt;mat-form-field&gt;
  &lt;input matInput type=&quot;string&quot; [ngModel]=&quot;element&quot; (change)=&quot;updateElement($event.target.value,index)&quot; name=&quot;element&quot; #field=&quot;ngModel&quot;&gt;
&lt;/mat-form-field&gt;
&lt;/div&gt;

In .ts file, write update function as

updateElement(value:string,index:number) {
  this.list[index] = value;
}

huangapple
  • 本文由 发表于 2023年3月8日 18:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定