Assign the output of a pipe in angular2 to a variable
Asked Answered
B

1

11

So I have a pipe like this:

 <ul *ngFor="#eachNumber of (NumberArray | MyPipe: '1')"></ul>

How can I assign the returned result of the pipe to a variable that can be used elsewhere in my code?

I've looked for a while and read about something called assign-local but couldnt find much information on it. Is there a obvious way to do this that I am just missing?

Beep answered 23/12, 2015 at 10:19 Comment(6)
What's the purpose? Why would you need that?Arvell
I was more just curious to see if it could be done.Beep
You can apply the pipe in your code, why applying it in markup to use the result in code?Arvell
I'm interested to know if there was ever a solution found. Gunter, the reason you might want that is so you aren't iterating over the list multiple times. It's wasted computation.Gorky
Instantiate the pipe in a component and call transform() on each array item.Rancher
To go along with @DanWilson's answer, the code will look like: formattedNumber = myPipe.transform(eachNumber)Intergrade
N
11

Just in case anyone stumbled upon this question. This is possible with Angular 4 now, with the updated as syntax :)

Neomaneomah answered 3/5, 2017 at 14:33 Comment(3)
Yep. Just came across this :)Ravin
Can you add more explanations/examples on how to do that ?Authorization
I haven't used Angular for a long time. But I was able to find an example on Angular doc, so this syntax should still be supported with newest version of Angular. <li *ngFor="let user of userObservable | async as users; index as i; first as isFirst"> {{i}}/{{users.length}}. {{user}} <span *ngIf="isFirst">default</span> </li> check angular.io/api/common/NgForOfNeomaneomah

© 2022 - 2024 — McMap. All rights reserved.