Hi Guys,
I have faced an error like "Cannot assign to 'name' because it is a constant or a read-only property." .
The code is as below
------------app.component.ts-----------------------------------
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html' //// refer app.component.html for property binding
})
export class AppComponent {
name="";
testEventBind(userName: string) {
name=userName;
}
}
I have faced an error like "Cannot assign to 'name' because it is a constant or a read-only property." .
The code is as below
------------app.component.ts-----------------------------------
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html' //// refer app.component.html for property binding
})
export class AppComponent {
name="";
testEventBind(userName: string) {
name=userName;
}
}
------------------------------------------------------------
I have highlighted the line which causes this error.
All you have to do is to use this keyword , change the highlighted line as below,
this.name=userName;
This should fix the issue.
No comments:
Post a Comment