What I am doing:
html file
<div *ngFor='let result of results'>
<div [ngClass]='{"myclass": someArray | inArray:result.id}'>
</div>
</div>
ts file:
someArray = [1, 2, 3]
results = [
{id:1, name:'abc'},
{id:2, name:'xyz'},
{id:4, name:'pqr'}]
Result: I never get assigned class.
How can I store result of pipe in html file using "as" to use it in ngClass?
inArray (pipe): Pipe returns true or false based on if value exists in array.
Referred Angular 2 add style based on pipe result:
Update 1:
inArray (pipe): Pipe returns -1 or index value based on if value exists in array.
UPDATE 2: stackblitz
If I'm not wrong, this is what you're looking for!
Sorry I had to take care of something and only got back now..
Check the StackBlitz
Only applies the class if the value is present.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'inArray' })
export class inArrayPipe implements PipeTransform {
transform(value: any, exponent: any): boolean {
let result = value.indexOf(exponent);
return result > -1;
}
}
The rest is your code.. but It's all working together in the Stackblitz example above.