The interface remains static even after the property has been modified

There is a function in my codebase called getData() that fetches data from an API. I am trying to call this function from a different component, which I will refer to as MainComponent and AnotherComponent for the sake of clarity:

import MainComponent from ./../main-component/main-component

...

export class AnotherComponent extends OnInit {
   ...

   constructor(private _maincomp: MainComponent){}

   onClick = () => {
      this._maincomp.getData();
   }
}

Although the data in the MainComponent property updates successfully, the view does not reflect these changes. How can I resolve this issue?

This is the html snippet from the MainComponent:

<div class="row table-container shadow rounded mt-4 ml-1">
   <table mat-table class="data-table mat-elevation-z10" [dataSource]="datas">
       <ng-container matColumnDef="Order ID">
           <mat-header-cell class="header order-id-col" *matHeaderCellDef>Order ID</mat-header-cell>
           <mat-cell class="order-id-col col-data" *matCellDef="let data">{{ data.order_id }}</mat-cell>
       </ng-container>

       //another ng-container goes here...

       <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
       <mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
   </table>
</div>

Here is the .ts file for the MainComponent:

export class MainComponent implements OnInit {

   displayedColumns = ['Order ID', 'Order By', 'Batch Kultur', 'Jenis Media', 'Jenis Vessel', 'Jumlah Ordered',
                    'Tanggal Order', 'Tanggal Pakai', 'Status', 'Action'];
   datas;
   filter;

   constructor(private _service: DataModelService) { }

   ngOnInit() {
       this.getData();
   }

   getData = () => {
       this._service.getData(1, 10, this.filter , 'media-request')
                .subscribe(
                    res => {
                        this.datas = res.data.data;
                    },
                    err => {
                        console.log(err);
                    }
                );
   }  
}

This is the AnotherComponent that invokes the MainComponent function:

import { MainComponent } from '../main-component/main-component.component';

export class MainComponent implements OnInit {

  constructor(public _maincomp: MainComponent) { }

  ngOnInit() {}


  onClick = () => {
      this._router.navigate([`dashboard/main-component`]);

      setTimeout(() => {
        this._maincomp.getData();
      }, 1000)
  }
}

Answer №1

Consider implementing ChangeDetectorRef:

constructor(
  private detector: ChangeDetectorRef,
  private _component: CustomComponent
) {}

onClick = () {
  this._component.fetchData();
  this.detector.markForCheck();
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The issue of HTTP parameters not being appended to the GET request was discovered

app.module.ts getHttpParams = () => { const httpParamsInstance = new HttpParams(); console.log(this.userForm.controls) Object.keys(this.userForm.controls).forEach(key => { console.log(this.userForm.get(key).value) const v ...

Why is the Zip archive downloader not functioning properly when using Node.js and Archiver (Unexpected end of archive error)?

Looking to download multiple files using archiver with express. The server should respond to a post request from the client by sending a .zip file. However, there seems to be an issue where WinRAR displays an error message "! 98I9ZOCR.zip:Unexpected end of ...

Different ways to tag users with the "@" symbol in Django

I've been diving into a project on django that has similarities to Instagram and Twitter. One crucial function it needs is the ability to mention users using "@" in text fields. After some research, I've come up empty-handed, except for libraries ...

Ways to retrieve the innerHTML content in Mozilla Firefox

Look at this HTML code snippet: <div id="divTest"> Hello <input type="text" id="txtID" value="" /> <input type="button" onclick="getForm();" value="Click" /> </div> Also, check out this JavaScript function for retrievi ...

Issue with bootstrap 4 CDN not functioning on Windows 7 operating system

No matter what I do, the CDN for Bootstrap 4 just won't cooperate with Windows 7. Oddly enough, it works perfectly fine on Windows 8. Here is the CDN link that I'm using: <!doctype html> <html lang="en> <head> <!-- Req ...

Utilizing regex patterns within Jqgrid

I am currently utilizing the JqGrid plugin (version 4.6.0 - jQuery Grid) to filter my results using specific fields. Here is an example of how the GUI appears: https://i.sstatic.net/BqGai.png With a large list of employees, I want the ability to filter t ...

I'm encountering error code TS2314: The generic type 'ɵɵDirectiveDeclaration' is requesting 6 type arguments. How should I go about resolving this issue?

https://i.sstatic.net/b4Y6d.png https://i.sstatic.net/CIDTm.png While working on my project, everything was running smoothly until I copied a template into another component. Suddenly, the new component failed to recognize Angular Material elements, caus ...

execute script upon changes to the DOM of the embedded vendor component

Incorporating a vendor component into MyComponent. @Component({ selector: 'my-component', template: `<vendor-component></vendor-component>` }) export class MyComponent { constructor() { } } Looking to execute some jQuery ope ...

What are the steps to create a custom progress bar using JavaScript?

I have experience with HTML and CSS. Below is the HTML code: <div id="wrapper"> <ul id="top"> <center><li><a href="#one" class="button">GENERATE</a></li></center> </ul> <div class="box" i ...

Retrieving package information from NPM using a web browser

I am currently developing a Chrome Web App that retrieves information from NPM. However, I have encountered issues due to Chrome adhering to the Access-Control-Allow-Origin flags set by websites. While I am able to access the following URL: http://regist ...

The "Splash Screen Div" page displayed during transitions and page loading

Creating a "Splash Screen Div" for a loading page involves waiting until everything is loaded and then hiding or moving the div off screen. Below is an example: index.html <div id="loading-Div"> <div id="bear-Logo"> < ...

Bootstrap 4 or ngBootstrap: A Comparison

I have a couple of inquiries: Firstly, I am wondering if ngBootstrap is compatible with Angular 4. While I've seen instances on Google where individuals have successfully used Angular 4 with ngBootstrap, the official site mentions dependencies on Ang ...

Sending an HTTP POST request from an Angular 6 application utilizing Sendgrid and a NodeJs Google Cloud Function results in a Error 405 message

Attempting to access an Email through Sendgrid from an Angular 6 application with minimal payload. Utilizing a Google Cloud Function for the request leads to a 405 error from the browser: 405 Blocked by CORS policy: Response to preflight request doesn&ap ...

The installation failed due to an unresolved dependency when running npm install

Upon performing a git clone, I encountered this error with my project after running npm install: npm ERR! code ERESOLVE npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0f1e1e43081c01001a0b000 ...

The text area is not increasing in size when using the " " for a new line

I have a code that enables Autocomplete input to add values chosen from the list to a textarea, with each value separated by a new line. While manually adding rows to the textarea by pressing Enter allows it to expand and fill in the new row and value, us ...

Is there a glitch preventing the connection between HTML and CSS in Notepad++ even though the link is correct?

I have encountered a puzzling situation that has left me scratching my head in confusion. Despite being a rookie, I've had experience with linking CSS files before and can't understand why it's not working this time around. This issue is per ...

Raycaster is unable to manipulate BoxMesh objects in any way

I've been working with the Physijs script for simulating physics, such as gravitation. I'm trying to use Raycaster from the THREE.js script to move objects within my scene. The issue I'm facing is that the Raycaster only moves objects (sim ...

How can a jQuery each loop be used to verify if at least one field has been completed?

Consider this HTML structure: <input type="text" name="input_1" class="required" value="" /> <input type="text" name="input_2" class="required" value="" /> <input type="text" name="input_3" class="required one" value="" /> <input type ...

Transmit information from node.js to the frontend utilizing only raw JavaScript

Seeking guidance on sending data object from a Node.js server to a JS file for frontend use. In the main.js file, DOM manipulation is performed. The following request is made: let dataName = []; let request = async ('http://localhost:3000/') =& ...

Using regex in Javascript to find and match an ID within a string

JavaScript: var data='<div id="hai">this is div</div>'; I am looking to retrieve only the ID "hai" using a regular expression in JavaScript. The expected output should be, var id = regularexpression(data); The variable id should n ...