0
*ngFor="let item of items | filter:{ name: searchString } : searchString as filteredItems"

and later

<button (click)="doSomething(filteredItems)>

filteredItems is just a local variable that can be used inside *ngFor. How can I bind filteredItems to a global variable so I can submit all filtered items later when I press the button?

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Jan Bürger
  • 111
  • 2
  • 9
  • My second pla, ` – Jan Bürger Jan 07 '20 at 15:42
  • may i ask why you are using a pipe to filter? for what you want to do wouldn't it be easier to just have a filter function that returns your array and then store that array as a parameter of your component? – Chirag Patel Jan 07 '20 at 15:42
  • I thought a pipe to filter is the way to go. I followed guides likes this: https://stackoverflow.com/questions/44769748/angular-4-pipe-filter and it works great, I am just having trouble to get the returns of it now later in the project. – Jan Bürger Jan 07 '20 at 15:46

1 Answers1

1

You can do something like the following `

<ng-container *ngIf=”(items | filter:searchString) as filteredItems”>

  <div *ngFor=”let item of filteredItems”>
    {{item}}
  </div>

  <p>Count: <b>{{filteredItems.length}}</b></p>
  
</ng-container>

then you will be able to use filteredItems as a variable.

However if you are wanting to run something on a button click, you shouldn't use Pipes, and instead write a function in your component.

Chirag Patel
  • 364
  • 2
  • 12