Search code examples
angulartypescriptcompiler-errorsangular2-components

Property toggleEdit is missing in type Employee", but it is a method, what to do? Angular2


I have an Employee component -

export class EmployeeComponent{
name:string;
    isEditOn:boolean;

    constructor(){}

     toggleEdit():void{
        this.isEditOn = !this.isEditOn;
    }
}

And EmployeeList Component like this -

export class EmployeeListComponent{
    employees: Employee[] = [];

    AddNewEmployee(){
        this.employees.push({name:"New Employee", isEditOn:false});
    }
}

I am getting error on the line where I am trying to push the new Employee in the list.

Property 'toggleEdit' is missing in type '{ name: string; isEditOn: false; }'.

But isn't it a method? Am I missing something here?


Solution

  • export class Employee {
    
        constructor(public name: string, public isEditOn: boolean){}
    
         toggleEdit():void{
            this.isEditOn = !this.isEditOn;
        }
    }

    export class EmployeeListComponent{
        employees: Employee[] = [];
    
        AddNewEmployee(){
            this.employees.push(new Employee("New Employee", false));
        }
    }