3

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

Always_a_learner
  • 4,585
  • 13
  • 63
  • 112

5 Answers5

5

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.

rmjoia
  • 962
  • 12
  • 21
  • Thanks a lot rmjoia for taking time out to answer this with usage of pipe. Could you check out fork where I have altered pipe and it stopped working. Thanks. https://stackblitz.com/edit/angular-nevhxm-so-at45c1 – Always_a_learner Apr 19 '19 at 13:16
  • @Harsimer has to return a boolean or a truthy value.. something is not working, I'll have to debug... please standby – rmjoia Apr 19 '19 at 13:18
  • 1
    I found the issue. My pipe is checking value incorrectly. item === value instead of item.value === value – Always_a_learner Apr 19 '19 at 13:19
  • same, and saved your fork :) glad I could help :) don't forget to mark the answer (you consider the best) as the answer :) – rmjoia Apr 19 '19 at 13:23
1

You can try something like this - bind the class name to the property binding and use your pipe

[class.myclass]='someArray | inArray:result.id'

Your pipe will return true or false based on the condition the class will be added to the specific html

Hope this helps - happy coding :)

Rahul
  • 2,040
  • 2
  • 11
  • 29
  • Rahul thanks for the answer. I have made a small update in Post. My pipe returns -1 or index value of key in array instead of true or false. – Always_a_learner Apr 19 '19 at 12:46
0

The pipes should be used to transform values.

Try using a simple function to check this:

inArray(value, theArray) {
   return theArray.includes(value);
}

and the template:

 <div *ngFor='let result of results'>
    <div [ngClass]='{"myclass": inArray(result, results)}'>
    </div>
 </div>

or even without the function:

 <div *ngFor='let result of results'>
    <div [ngClass]='{"myclass": results.includes(result)'>
    </div>
 </div>

Good luck!

Andrew Radulescu
  • 1,862
  • 13
  • 21
  • 1
    Andrew Radulescu, but isn't it bad practice to use function directly in html to assign a class as it will cause issue with change detection? – Always_a_learner Apr 19 '19 at 12:26
  • 1
    No, it is not. This will re-render and re-call the function only when the results array is changed. It's a common usage :). More, the pipe will do exactly the same `for each element will run a function to transform the given values`. – Andrew Radulescu Apr 19 '19 at 12:27
  • Andrew Radulescu Thanks for clarifying, but sometimes I have expirienced "expression checked after value changed" error while using functions directly in html template. So I was trying to avoid usage of functions directly in html. – Always_a_learner Apr 19 '19 at 12:48
  • 1
    This would happen if you update the `results` array in the function that checks that array. Won't happen in your case. – Andrew Radulescu Apr 19 '19 at 12:51
0

In my case I used a directive with inputs, when the directive inputs change, the required style can be calculated and applied on the directive input. Taken from answers here: Angular2 Styles in a Directive

Eli
  • 1,670
  • 1
  • 20
  • 23
-1

You can use function for this : [ngClass]="isValueInArray()" and return value true/false in that function

Shifali singla
  • 76
  • 1
  • 2
  • 8