How to use Angular Signals with Material Table data source?

Discussão em 'Angular' iniciado por Oleksii Yurchenko, Setembro 24, 2024.

  1. I'm trying to use new angular "feature" - signals. Inside some service I have a signal:

    failedTasks = computed(() => this.tasksState().failedTasks);


    And then, I want to connect that signal to the table data source inside component. Previously, using observables, the code was like that:

    dataSource = new MatTableDataSource<FailedTask>([]);

    constructor(tasksService: TasksService){
    this.tasksService.failedTasks$.subscribe(tasks => this.dataSource.data = tasks)
    }


    but with signals I have no idea how to do the same except using "effect" like that:

    constructor(tasksService: TasksService) {
    effect(() => {
    this.dataSource.data = this.tasksService.failedTasks();
    });
    }


    What do you think about this approach? According to official docs "Effects are rarely needed in most application code", but how to implement the logic when we need to subscribe some source inside component.ts?

    Continue reading...

Compartilhe esta Página