Error when using limitTo as a filter in ionic 2

1

I have a problem and I want to limit the number of letters that a property of an object represents using limitTo , I get an error

  

Error: Uncaught (in promise): Error: Template parse errors: The pipe   'limitTo' could not be found ("click)=" openCart () ">

     {{[ERROR - >] producto.description | limitTo: 50}}

Html

<p class=" description">{{ producto.description | limitTo: 50 }}</p>

js

productos.description ="Product Description este 
      es mi texto describiendo las caracteristicas completas del 
      articulo, tales como: peso, tamaño, color, etc.";

See documentation - > link

    
asked by Daniel Enrique Rodriguez Caste 23.06.2017 в 19:25
source

1 answer

2

I make this answer based on your comment:

In angular 2, the Pipe filter no longer exists, therefore, you need a custom Pipe (I hope you do not mind, I made some changes to your code):

miPipes.ts:

import { Pipe, PipeTransform } from '@angular/core'; 
@Pipe({ name: 'miLimitTo' }) 

export class LimitToPipe implements PipeTransform {
  transform(value: string, limite: number, desde: number = 0): string { 
      return (value.length > limite)? value.substring(desde, limite) + '...': value.substring(desde, limite); 
  } 
} 

app.module.ts:

...
import { LimitToPipe } from '../pipes/miPipes'
...
@NgModule({
  imports: [ BrowserModule ],
  declarations: [ App, LimitToPipe ],
  bootstrap: [ App ],
  providers: [],
  exports: [LimitToPipe]
})

Html:

<h2>{{name | miLimitTo:100:10}}</h2>
<h2>{{name | miLimitTo:100}}</h2>

Functional example in Plunkr: link

Thank you very much @DanielEnriqueRodriguezCaste

    
answered by 24.06.2017 / 17:35
source