0

I have an issue with my Angular toDoApp project. I have 3 components (app,task-form.task-list). App component receives the final logic from other components. But when I try to push the final object into array, it says that the object values are undefined.

   export interface  toDoInterface {
        tittle: string,
        description:string;
    }

 ----------

 export class AppComponent {

  toDoList = [];      //**It returns undefined value**

  newToDo (formData: toDoInterface) {
    this.toDoList.push({
    tittle: formData.tittle,
    description: formData.description
  });  
 }
}

----------

  export class TaskFormComponent implements OnInit {

 @Output() toDo = new EventEmitter<{newTittleValue: string, newDescValue:string}>();

  tittleValue = '';
  descValue = '';

  constructor() { }

  ngOnInit() {
  }

  addToList () {

  this.toDo.emit({newTittleValue:this.tittleValue,newDescValue:this.descValue})

  }  
}


----------

export class TaskListComponent implements OnInit {

 @Input() name: toDoInterface;

  constructor() { }

  ngOnInit() {
  }

}
3
  • Try: toDoList: any[] = []; Just a suggestion. Commented Jul 8, 2018 at 21:02
  • still returns undefined values. Commented Jul 8, 2018 at 21:20
  • Are you handling the $event response param correctly?? angular.io/api/core/EventEmitter Commented Jul 8, 2018 at 21:45

1 Answer 1

2

You are emitting a different object then you expecting to receive in your app.component.ts file newToDo method.

You emit the following object:

{
  newTittleValue: string;
  newDescValue: string;
}

But when you add it to your list you use the following structure (in your case the toDoInterface interface):

{
  tittle: string;
  description: string;
}

You want to use a parameter from the passed in formData object that doesn't exist.

The solutions is simple. Use the interface as the type of your EventEmitter.

Like so and so:

@Output() toDo = new EventEmitter<toDoInterface>();

tittleValue = '';
descValue = '';    

addToList() {
  this.toDo.emit({ tittle: this.tittleValue, description: this.descValue });
}

I also made a stackblitz project, so you can see it in action.

Sign up to request clarification or add additional context in comments.

1 Comment

@Nosheep if so please mark my answer as the correct one, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.