I am completly new in angular. I have 2 components, one for displaying list of taks, one for adding new one. They use one service. This is my function in TasksComponent:
getTasks(): void {
this.tasksService.getTasks()
.subscribe(
tasks => this.tasks = tasks,
err => {
console.log(err);
});
}
And I have also function for adding new Task in TasksInputFormComponent. So I need to call getTasks() function from another component using shared service. I have found this question How to call component method from service? (angular2) and have tried to do something like in example. I don't understand how to do it right. In tasksService:
private componentMethodCallSource = new Subject<any>();
componentMethodCalled$ = this.componentMethodCallSource.asObservable();
In TasksComponent:
constructor(private tasksService: TasksService) {
this.tasksService.componentMethodCalled$.subscribe(
() => {
this.tasksService.getTasks()
.subscribe(
tasks => this.tasks = tasks,
err => {
console.log(err);
});
}
);
}
In TasksInputFormComponent:
addTask(task) {
...
this.tasksService.addTask(...);
this.tasksService.callComponentMethod();
}
But this code looks absolutly strange and doesn't work. I can't find how to do it properly.