-1

I am new to angualr 8 and haven't used custom pipes. Kindly help on this. How to write custom pipe for 'limitTo : articles.articles.limit ? articles.articles.length : 10'

<div *ngFor="let section_articles of articles.articles | limitTo : articles.articles.limit ? articles.articles.length : 10">
3103
  • 59
  • 1
  • 7
  • You asked this a few hours back: https://stackoverflow.com/questions/58073656/using-custom-pipe-with-ternary-operator-for-ngfor Instead of posting a new question, which is also not a [mcve] again, you could answer my [question](https://stackoverflow.com/questions/58073656/using-custom-pipe-with-ternary-operator-for-ngfor#comment102546073_58073656) there. – AT82 Sep 24 '19 at 12:10

2 Answers2

0

I suppose that you want to make an iterable table with only 10 items ? better make a .slice(0,10) in the end of your method. if not you can parameterizing a pipe or customizate it following this example:

import { Pipe, PipeTransform } from '@angular/core';
/*
 * Raise the value exponentially
 * Takes an exponent argument that defaults to 1.
 * Usage:
 *   value | exponentialStrength:exponent
 * Example:
 *   {{ 2 | exponentialStrength:10 }}
 *   formats to: 1024
*/
@Pipe({name: 'exponentialStrength'})
export class ExponentialStrengthPipe implements PipeTransform {
  transform(value: number, exponent?: number): number {
    return Math.pow(value, isNaN(exponent) ? 1 : exponent);
  }
}

For example: using Interpolation:

<p>The hero's birthday is {{ birthday | date:"MM/dd/yy" }} </p>

template: `
  <p>The hero's birthday is {{ birthday | date:format }}</p>
  <button (click)="toggleFormat()">Toggle Format</button>
`
export class HeroBirthday2Component {
  birthday = new Date(1988, 3, 15); // April 15, 1988
  toggle = true; // start with true == shortDate

  get format()   { return this.toggle ? 'shortDate' : 'fullDate'; }
  toggleFormat() { this.toggle = !this.toggle; }
}

i take this from angular docs: parameterizing a pipe

Qiqke
  • 486
  • 5
  • 19
0

Solution Worked:

<div *ngFor="let section_articles of (articles.articles.limit ? (articles.articles | slice: 0 : articles.articles.length) : (articles.articles | slice: 0 : 10))">
3103
  • 59
  • 1
  • 7