How can we effectively manage the array of objects passed to the Angular mat-select-search drop-down component and ensure proper control over the function execution?

After successfully implementing the mat-select-search component with live search functionality by following guides on Angular ngx-mat-select-search Custom Component and the documentation for ngx-mat-select-search, everything was working smoothly until I tried to introduce dynamic data.

In the Child component, an array of objects is retrieved:

@Input() sites: any[];

This array is then accessed by another function within the component:

export class SiteDropdownComponent<T> implements OnInit, OnDestroy, AfterViewInit {    

 ngOnInit(): void {
   // set initial selection
   this.siteCtrl.setValue(this.sites[1]);
   // load the initial site list
   this.filteredSites.next(this.sites.slice());
   // listen for search field value changes
   this.siteFilterCtrl.valueChanges
     .pipe(takeUntil(this.onDestroy))
     .subscribe(() => {
       this.filterSites();
     });}}

The Parent component incorporates the Child component as follows:

<app-site-dropdown [siteCtrl]="sitesForm.get('site')" [sites]="sites" (sel)="changeCoun($event)"></app-site-dropdown>

When providing objects directly like in the AppComponent works fine:

export class AppComponent implements OnInit, AfterViewInit, OnDestroy {  

sites: any[] = [
  {id: 'item1', name: 'item1'},
  {id: 'item2', name: 'item2'},
  {id: 'item3', name: 'item3'}
]; 

However, when attempting to populate the 'sites' array through a server request with a delay:

getSitesList() {
  this.subs.add(this.http.post(this.count, 'url/listSites').subscribe(data => {
  this.sites = data.sites;
  console.log(this.sites);
}));

An issue arises during the delay time where the SiteDropdownComponent renders before the 'sites' array is populated, resulting in errors such as 'sites are undefined' when trying to use functions that rely on 'sites':

ngOnInit() {
  // set initial selection
  this.siteCtrl.setValue(this.sites[1]);
  // load the initial site list
  this.filteredSites.next(this.sites.slice());
}

Is there a more optimal way or function to address this issue? Considering calling these functions at another point in the code, without resorting to using setTimeOut(). A detailed explanation of the SiteDropdownComponent can be found in the provided link above.

Answer №1

Ensure that your site-dropdown is only loaded when the variable sites is set by using *ngIf.

<app-site-Dropdown *ngIf="sites" [siteCtrl]="sitesForm.get('site')" [sites]="sites" (sel)="changeCoun($event)"></app-site-dropdown>

For more information on NgIf, refer to its documentation.


It could also be a problem if the sites array is not initialized properly. If you are populating the data through a request, make sure to initialize the array beforehand:

sites: any[] = [];

Answer №2

an alternative approach is to utilize a setter method

export class SiteDropdownComponent<T> implements OnInit, OnDestroy, AfterViewInit {  

  @Input() set sites(sites: T[]){
    this._sites = sites;
    this.initializeFormControls();
  };
  get sites(): T[] {
    return this._sites;
  }
  private _sites: T[];

  private initializeFormControls() {
     // set initial selection
   this.siteCtrl.setValue(this.sites[1]);
   // load the initial site list
   this.filteredSites.next(this.sites.slice());
   // listen for search field value changes
   this.siteFilterCtrl.valueChanges
     .pipe(takeUntil(this.onDestroy))
     .subscribe(() => {
       this.filterSites();
     });}}
  }

}

using this method enables the input to be updated dynamically

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

Access denied: Unable to rename directory '/usr/local/lib/node_modules/expo-cli' due to permission restrictions

After encountering an error that appeared to be related to permissions, I spent some time troubleshooting and finally found a solution. I wanted to share it here in case it can help others facing the same issue. If anyone has alternative solutions, please ...

Encountering a problem when attempting to start a new project using Ionic3

Currently, I am in the process of setting up a fresh project on my Windows 10 system using Ionic along with all its necessary dependencies and modules fully installed. However, upon executing the following command to create an app: ionic start my-app An ...

Configuring Stylelint in a NextJS project using Emotionjs

I recently encountered an issue while trying to integrate Stylelint into a new NextJS Typescript project with EmotionJS. Many rules were not working in my styles files, and the only error I could identify was Unknown word CssSyntaxError. This particular U ...

Encountering the message "npm ERR! missing script: start" following the update to .Net 3.0

Previously, the ASP.Net Core 2.2 code (upgraded from 2.1) did not include a start script in package.json. https://github.com/TrilonIO/aspnetcore-angular-universal/blob/master/package.json Upon upgrading to ASP.Net Core 3.0, it now requires a start script ...

Select all entities in TypeORM except for the ones where the id is not equal to a specific condition

In my scenario, I am dealing with two entities: @Entity() export class Point { @PrimaryGeneratedColumn('uuid') id: string; // some other stuff } @Entity() export class Product { ...

Dealing with server-side errors while utilizing react-query and formik

This login page utilizes formik and I am encountering some issues: const handleLogin = () => { const login = useLoginMutation(); return ( <div> <Formik initialValues={{ email: "", password: "" }} ...

Creating Dynamic Forms in React with Typescript: A Step-by-Step Guide to Adding Form Elements with an onClick Event Handler

I am looking to create a dynamic generation of TextFields and then store their values in an array within the state. Here are my imports: import TextField from '@material-ui/core/TextField'; import Button from '@material-ui/core/Button&apos ...

Execute a function using a click event within a statement

Is it possible to trigger a function with parameters based on the result of a statement? For example, can we achieve something like this: (click)="datavalue.elementDataCollection.length > 1 ? AddNewDialog (datavalue,datavalue.COCLabel,mainindex,i) : r ...

TypeScript Compile Error: The property is not available in the 'IStateParamsService' type

My client project heavily utilizes TypeScript. Currently, I am encountering a technical issue. Within my HTML code, I have an anchor tag as shown below: <a class="btn btn-default mrm" ui-sref="course-detail({courseId: '{{c.Id}}'})">Detail ...

Why does Typescript's 'await' seem to not wait as expected?

Apologies for the rookie mistake, I am currently transitioning from a C# background to Ionic, which may be causing some confusion on my end. I'm working on retrieving a stored token from Ionic storage but I'm struggling with understanding promise ...

The Ionic Menu fails to display when the modal is chosen

Upon logging into the app, users encounter a modal that prompts them to enter data before proceeding to the logged-in page. This page is supposed to display a menu, which I have enabled by calling this.menu.enable on the home page. However, despite enablin ...

Create and export a global function in your webpack configuration file (webpack.config.js) that can be accessed and utilized

Looking to dive into webpack for the first time. I am interested in exporting a global function, akin to how variables are exported using webpack.EnvironmentPlugin, in order to utilize it in typescript. Experimented with the code snippet below just to und ...

Dynamic URL used in TRPC queries`

Is there a way to query a single post in TRPC and Next JS based on a dynamic url without using SSR or SSG? I have tried adding as string to router.query.id but encountered a TRPC error (only happening during the first load) because router.query.id is ini ...

What are some key indicators in the source code that differentiate TypeScript from JavaScript?

Reviewing some code on Github, I am looking for ways to quickly determine whether the script is written in JavaScript or TypeScript. Are there any simple tips or hints that can help with this? For instance, when examining an array declaration like the on ...

Is it possible to convert a DynamoDB AttributeMap type into an interface?

Assume I define a TypeScript interface like this: interface IPerson { id: string, name: string } If I perform a table scan on the 'persons' table in DynamoDB, my goal is to achieve the following: const client = new AWS.DynamoDB.Documen ...

Angular 6 is throwing an error stating that the 'publish' property is not found on the type Observable<string>

Currently, I am working on my initial Angular project using Angular 6. To streamline the process of handling errors in one central location, I have decided to incorporate Error Handling by referencing this insightful article on Medium From the code snipp ...

Accessing data from an API and showcasing information on a chart using Angular

I'm currently developing a dashboard application that requires me to showcase statistics and data extracted from my MongoDB in various types of charts and maps using Angular and Spring Boot. The issue I'm facing is that when attempting to consume ...

Ways to implement a component service interface in Angular

My goal is to create a component that enforces certain design guidelines, particularly focusing on the constructor. //cool-header.component.ts @Component({ selector: 'cool-header', moduleId: module.id, templateUrl: './header.compone ...

Hold on for the processing of a CSV document

I am attempting to utilize the "csv-parse" library in Typescript to read a csv file by creating an observable. The code provided uses fs.createReadStream to read the file. I am looking to return the observable and subscribe to it, but it seems that the p ...

What potential issues should I be aware of when I install new node packages?

I encountered an error message when trying to install a Node Package. Running npm install shows that everything is up to date. https://i.stack.imgur.com/d78hf.png ...