Cannot assign value "undefined" to template variable "buttonName". Template variables are read-only. Angular/TypeScript

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

Cannot assign value "undefined" to template variable "buttonName". Template variables are read-only. Angular/TypeScript

问题

所以我想说我是Angular的新手。

app.component.ts;

model = new Model();
addItem(value: string) {
    if (value !== "") {
        this.model.items.push(new TodoItems(value, false));
    }
}

model.ts;

export class TodoItems {
    description;
    action;

    constructor(description: string, action: boolean) {
        this.description = description;
        this.action = action;
    }
}

app.component.html;

<input type="text" #buttonName class="form-control">
<div class="input-group-append">
    <button class="btn btn-primary" (click)="addItem(buttonName.value); buttonName=''">Add</button>
</div>

我如何解决这个错误?

英文:

So I want to say Im new for Angular.

app.component.ts;

 model = new Model();
 addItem(value:string){
    if(value!=&quot;&quot;)
    {this.model.items.push(new TodoItems(value,false));}

model.ts;

export class TodoItems{
        description;
        action;

    constructor(description: string,action: boolean){
        this.description= description;
        this.action = action;
    }  
 }

app.component.html;

&lt;input type=&quot;text&quot; #buttonName class=&quot;form-control&quot;&gt;
          &lt;div class=&quot;input-group-append&quot;&gt;
            &lt;button class=&quot;btn btn-primary&quot; (click)=&quot;addItem(buttonName.value); buttonName=&#39;&#39;;&quot;&gt;Add&lt;/button&gt;
          &lt;/div&gt;

How I can solve this error?

答案1

得分: 1

你应该在组件中处理 todoText 状态的更改(尽管我看不到它的任何用途),而不是在模板中。所以我猜你想要清除内容,这样你可以在执行的函数中处理它。
我稍微重写了组件。

如果你继续收到错误,请分享 app.component,因为它不太清楚是否定义正确。

app.component.ts

export class AppComponent {
  todoText = '';
  model = new Model();

  public addItem(value: string) {
    if (value != '') {
      this.todoText = ''; // 在这里管理 todoText
      this.model.items.push(new TodoItems(value, false));
    }
  }
}

app.component.html

<input type="text" #buttonName class="form-control" />
<div class="input-group-append">
  <button
    class="btn btn-primary"
    (click)="addItem(buttonName.value);"
  >
    Add
  </button>
</div>
英文:

You should handle the shange of status of todoText in the component (Even though I don't see any use of it), not the template. So I'm guessing you want to clear the content, so you can handle it in the function you execute.
I rewrote the component a bit.

If you keep getting the error, please share app.component, since it isn't that clear if it is well defined.

app.component.ts

export class AppComponent {
  todoText = &#39;&#39;;
  model = new Model();

  public addItem(value: string) {
    if (value != &#39;&#39;) {
      this.todoText = &#39;&#39;; //Manages the todoText here
      this.model.items.push(new TodoItems(value,false));
    }
  }
}

app.component.html

&lt;input type=&quot;text&quot; #buttonName class=&quot;form-control&quot; /&gt;
&lt;div class=&quot;input-group-append&quot;&gt;
  &lt;button
    class=&quot;btn btn-primary&quot;
    (click)=&quot;addItem(buttonName.value);&quot;
  &gt;
    Add
  &lt;/button&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月1日 07:47:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377903.html
匿名

发表评论

匿名网友

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

确定