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

EsprnzJK

So I want to say Im new for 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>

How I can solve this error?

IAM5K

Based on the code you provided, it seems like you are trying to assign a value to the template variable buttonName in your (click) event handler. As template variables are read-only and cannot be directly assigned a value.

To fix the error, you can modify your code as follows:

  1. Remove the reference to buttonName in the (click) event handler.
<button class="btn btn-primary" (click)="addItem(); todoText='';">Add</button>
  1. Update the addItem method in your component TypeScript file to retrieve the value from the buttonName template variable.
addItem() {
  const inputValue = this.buttonName.nativeElement.value;
  if (inputValue !== "") {
    this.model.items.push(new TodoItems(inputValue, false));
  }
}

Make sure to import the ViewChild decorator from @angular/core and add it to your component class.

import { Component, ViewChild, ElementRef } from '@angular/core';

// ...

export class YourComponent {
  @ViewChild('buttonName') buttonName: ElementRef;

  // Rest of your code
}

By using the ViewChild decorator and ElementRef, you can access the value of the buttonName input element in the addItem method and add it to your model accordingly.

Hpoe It helps else you can comment back for clarifications. If it helps please consider accepting.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

After upgrading angular from 8 to 9 error is throwing "Cannot assign value "undefined" to template variable "limit"

Error: Cannot assign value "$event" to template variable Angular 10

Getting error "Cannot read property 'template' of undefined"

Assign a value to a variable in the template - Angular7

assign variables to breakpoints in template

Assign and check variable in Template

Variable in template is undefined

ng-template Cannot read property 'selected' of undefined on checkbox

Angular5 - TypeError: Cannot read property 'template' of undefined

Angular6 - TypeError: Cannot read property 'template' of undefined

Exception in template helper: TypeError: Cannot read property 'mergedSchema' of undefined

ERROR TypeError: Cannot read property 'matches' of undefined at AppComponent_Template

Assign response to local variable throws Cannot read property 'length' of undefined

How can I assign value to a template only once?

Assign value of a column to variable in sql use jinja template language

Is it possible to assign a value to a variable with let in an angular component template?

Assign type to a variable and pass it to template

Possible? Assign blade template to variable

How to assign a variable in MVEL Template

Assign 0 to template if no value is present

Template variable, read a custom instance directly in template

Cannot assign to read only property

Magento - undefined variable this in block template

Variable accessible in component but undefined in template

C++ template to read value from member variable or member function

assign javascript variable to django template variable

Is there an equivalent of a "possibly undefined" value in a template?

Dynamic Template and undefined attribute value

Assign variables to child template in {% include %} tag Django