简体   繁体   中英

Update array in forEach angularjs

I am having a problem when trying to filter an array in angular. I'm using typescript.

I have a parent page that contains a directive. The directive has a property of an Array of items which it displays in a datatable.

On the parent page, I want to filter the list that is being passed to the directive. Here is how I am doing it....

<table items="vm.items"></table>

In my parent controller, I have a button which when you press it executes the following function:

applyFilters() {
   var filteredItems=[];
   this.items.forEach((value, key) => {
      if (value.item!== 'test') {
         this.filteredItems.push(value);
      }
   });
   console.log(this.filteredItems);
   this.items = this.filteredItems;
}

But the value in the directive does not update when I update the filter?

What am I doing wrong here?

Here:

 if (value.item!== 'test') {
     this.filteredItems.push(value);
 }

The variable filteredItems is defined through var filteredItems = []; , while you assign through this.filteredItems . Just use:

filteredItems.push(value);
...
this.items = filteredItems;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM