Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass dynamic value to pipe in Angular 8

Tags:

angular

How can i pass dynamic value to pipe in Angular.

<tr *ngFor="let users of Users | sortpipe: 'name'">
        <td>{{ users.name }}</td>
        <td>{{ users.email }}</td>
        <td>{{ users.mobile }}</td>
        <td><input type="button" (click)="getUser(users._id)" class="btn btn-info" value="Edit"></td>
        <td><input type="button" (click)="deleteUser(users._id)" class="btn btn-danger" value="Delete"></td>
</tr>

I would like to pass dynamic value at sortpipe: 'name' instead of name on click event.

like image 584
Savan Padaliya Avatar asked Oct 12 '25 20:10

Savan Padaliya


2 Answers

you just create a property and pass that property as pipe parameter when the proprty change this will update the pipe parameter value

<div>
  set sort key
    <input type="button" name="key" (click)="name ='name'" value="name">
    <input type="button" name="key" (click)="name ='mobile'" value="mobile">
</div>

<div>
  method 
  <label >
    <input type="radio" name="method" [(ngModel)]="method" value="asc"> ASC
    </label>

  <label >
    <input type="radio" name="method" [(ngModel)]="method" value="desc"> DESC
    </label>
</div>

<tr *ngFor="let users of Users | sort: name : method ">
  ....
</tr>

component

export class AppComponent  {
 name = "name"; // 👈
 method="asc"  //  👈

  Users = [...]
}

demo 🚀

like image 157
Muhammed Albarmavi Avatar answered Oct 14 '25 16:10

Muhammed Albarmavi


Your template.html... change 'name' to class object reference name, and then in your component you have to define this class member variable, and assign value to it...

<tr *ngFor="let users of Users | sortpipe: name " >
    <td>{{ users.name }}</td>
    <td>{{ users.email }}</td>
    <td>{{ users.mobile }}</td>
    <td><input type="button" (click)="getUser(users._id)" 
                 class="btn btn-info" value="Edit">
    </td>
    <td><input type="button" (click)="deleteUser(users._id)" 
                 class="btn btn-danger" value="Delete">
    </td>
</tr>

and in your component.ts file...

export class AppComponent {
     name = 'name'
}
like image 25
GaurangDhorda Avatar answered Oct 14 '25 17:10

GaurangDhorda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!