0

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?

1
  • 1
    this.filteredItems != var filteredItems Commented Sep 8, 2015 at 2:07

1 Answer 1

0

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Also he probably has this.filteredItems defined. He should delete that.
I still can get it to update the table in the directive. It looks like this now. <code> export class Controller implements IController { items: Models.IItem[]; filteredItems: Models.IItem[];applyFilters() { this.items.forEach((value, key) => { if (value.category!== 'test') { this.filteredItems.push(value); } }); // this.Items= this.filteredItems }} <code> this.items is still not updating in the view
The array itself is getting updated , but the property in the directive is not.
Well, maybe put some JSFiddle will help a lot. I cannot so figure out what you mean...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.