2

I'm new in angular. Have such pipe:

import { Pipe, PipeTransform } from '@angular/core';
import * as _ from 'lodash';

@Pipe({ name: 'arrayToString' })
export class ArrayToStringPipe implements PipeTransform {
transform(value: any[], name: string): string {
    console.log('----');
    if (_.isEmpty(value))
        return '';

    var result = _.join(_.map(value, function(e) { return name ? e[name] : e; }), ',');
    return result;
   }
}

Have record into declaration part of module file:

import { ArrayToStringPipe } from './pipes/arrayToString.pipe';
...
@NgModule({
imports: [
    ...
],
declarations: [
    ...
    ArrayToStringPipe
],
providers: [
],
exports: [
    ...
    ArrayToStringPipe
]
})
export class SharedModule {
}

And use pipe in other module:

import { SharedModule } from '../shared/shared.module';
@NgModule({
imports: [
   ...
    SharedModule,

],
declarations: [
    ...
],
exports: [
],
entryComponents: [
   ...
],
providers: [
    ...
]
})

and html

 <div *ngSwitchCase="Tags">{{ employee[field.Name] | arrayToString}}</div>

But, in browser I see (there is no errors in console):

[object Object],[object Object],[object Object]

I expect to see: tag1, tag2, tag3.

Any idea, why doesn't pipe work?

EDIT:

Full html

<div *ngIf="!(employees | isEmpty)" class="employee-list">
<table class="b-table">
                <thead>
                    <tr>
                        <th *ngFor="let field of rightGridFields" [ngClass]="{'sorted-column': filter.SortColumn === field.Name }">
                            <a *ngIf="field.Sortable" [ngClass]="{'asc': !filter.SortDescending, 'desc': filter.SortDescending}" (click)="sort(field.Name)">
                                {{field.DisplayName}}
                            </a>
                            <span *ngIf="!field.Sortable">{{ field.DisplayName }}</span>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr *ngFor="let employee of employees" (mouseover)="employee.hover = true" (mouseleave)="employee.hover = false" [ngClass]="{'selected': employee.selected,'hovered': employee.hover }">
                        <td *ngFor="let field of rightGridFields" [ngSwitch]="field.Name">
                            <div *ngSwitchCase="Tags">{{ employee[field.Name] | arrayToString}}</div>
                            <div *ngSwitchCase="Birthday">{{ (employee[field.Name] ? employee[field.Name]:'') | formatLocalDate}}</div>
                            <div *ngSwitchDefault>{{ employee[field.Name] }}</div>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>

EDIT 2: When remove *ngSwitchCase="Tags", pipe begin work. It is bug of angular?

Roma Ruzich
  • 692
  • 5
  • 18

2 Answers2

1

try to run this code after removing [] (i.e. value:any[] to value:any) this from the transform function definition

transform(value: any[], name: string): string {

hope it will work because any type include every data type that may be of primitive , array or object so no need to specify any[]

Sunny Goel
  • 1,982
  • 2
  • 15
  • 21
1

Thanks everybody. My mistake is very stupid. Resolve of problem is:

<div *ngSwitchCase="'Tags'">

It is needed to pass '' for Tags.

Roma Ruzich
  • 692
  • 5
  • 18
  • Yeah, all template deriectives take template expressions. They are like property bindings with [], but instead of the brakets you have to write asterix. – Totati Oct 11 '19 at 14:40