Search code examples
typescriptserviceangular7ngoninit

Delete empty row created by subscription to service on ngOnInit()


I have a Material Data table, which the user can fill and empty by himself. The data, which gets into the table gets triggered by a service, which I initialized in the table component with ngOnInit().

Code:

  ngOnInit() {
    this._AS.transaction$.subscribe( transaction => {
        this.transaction = transaction;
        this.temp = this.dataSource.data.slice();
        this.temp.push(this.transaction); // Stop this call on init somehow?
        this.dataSource.data = this.temp;
        this.ref.detectChanges();
        console.log(this.transaction);
      }
    );
  }

The problem is: There is always an empty row which gets pushed by the call this.temp.push(this.transaction). Is there a way to stop this call for the first time or something similar?


Solution

  • I just used an alternative way: After pushing the empty Transaction into the table I pop it afterwards, so that the table is empty again. Pushing data works now without problems.

      ngOnInit() {
          this._AS.transaction$.subscribe(transaction => {
              this.transaction = transaction;
              this.temp = this.dataSource.data.slice();
              this.temp.push(this.transaction);
              this.dataSource.data = this.temp;
              this.ref.detectChanges();
            }
          );
          this.temp = this.dataSource.data.slice();
          this.temp.pop();
          this.dataSource.data = this.temp;
          this.ref.detectChanges();
      }