Using custom pipe in angular 2, unable to load the data on load of the html throws error cannot read property of undefined -
my template:
<tbody *ngfor="let abclist of abclists | filterdata: srchterm; let = index"> {{abclist .name}} </tbody>`
data of abclists:
abclists = [ { 'id': 1, 'date': '02/04/2017', 'name': 'the hero', 'size': '1.8 gb', 'network': 'hfg', 'services': 'wughf', 'content_owner': 'any', 'contact_email': 'iwygf' }]
and custom filter
import { pipe, pipetransform } '@angular/core'; @pipe({ name: 'filterdata' }) export class filterpipecomponent implements pipetransform { transform (value: any[], args: any[]){ const search = args[0].value; console.log(search) if (!search) return value; return value.filter(item => (item.name == search || item.services == search || item.content_type == search || item.network == search || item.size == search || item.service == search) ); } }
on loading component on console error thrown :
abccomponent - inline template:56:27 caused by: cannot read property '0' of undefined
you can try ...agrs
uncertain counts of parameters. you have handle non parameter branch anyway, check whether parameter exists before using it.
see below example:
function test(...args) { console.log(args.length); } test(); test('a'); test('a', 'b'); test('a', 'b', 'c');
add sample using array.some
in array.filter
in order filter multiple keywords.
function tranform(input, args) { return input.filter(item => { return args.some(arg => { return item.field1 === arg || item.field2 === arg; }); }); } var arr = [ { field1: 'test1', field2: 'test2' },{ field1: 'test11', field2: 'test22' },{ field1: 'test33', field2: 'test44' } ]; console.log(tranform(arr, ['test1', 'test11']));
wiki
Comments
Post a Comment