-3

First time making a question here, in StackOverflow...

I'm trying to call a pipe that has two arguments.

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'showDateIntervenientValueForDesconhecido'
})
export class ShowDateIntervenientValueForDesconhecidoPipe implements PipeTransform {

  transform(dDataRecibo: string, monthlyYields: string[]): any {
    let i = 0;
    let bothAreNotZero = true;
    let yieldIntervenientValue = "";

    while (i < monthlyYields.length && bothAreNotZero) {
      if (monthlyYields[i]['nTitular'] === "0"
      && monthlyYields[i]['nAvalista'] === "0"
      && monthlyYields[i]['dDataRecibo'] === dDataRecibo) {
        yieldIntervenientValue = monthlyYields[i]['mRendimentoLiquidoMensal'];
        bothAreNotZero = false;
      }
      i++;
    }
  }
}

When I call it with one argument, Angular 6 recognizes the pipe.

<table mat-table [dataSource]="arrayMonthlyYields" id="rendimentosTable"
[hidden]="arrayMonthlyYields.length === 0">
  <ng-container matColumnDef="Desconhecido">
    <th class="text-center" mat-header-cell *matHeaderCellDef>Desconhecido</th>
    <td mat-cell *matCellDef="let dDataRecibo">{{ dDataRecibo | showDateIntervenientValueForDesconhecido }}</td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="colsForRendimentosPatrimonio"></tr>
  <tr mat-row *matRowDef="let row; columns: colsForRendimentosPatrimonio"></tr>
</table>

But, when I call it with two arguments, Angular does not recognize showDateIntervenientValueForDesconhecido as a pipe.

<table mat-table [dataSource]="arrayMonthlyYields" id="rendimentosTable"
[hidden]="arrayMonthlyYields.length === 0">
  <ng-container matColumnDef="Desconhecido">
    <th class="text-center" mat-header-cell *matHeaderCellDef>Desconhecido</th>
    <td mat-cell *matCellDef="let dDataRecibo">{{ dDataRecibo | showDateIntervenientValueForDesconhecido: monthlyYields }}</td>
  </ng-container>

  <tr mat-header-row *matHeaderRowDef="colsForRendimentosPatrimonio"></tr>
  <tr mat-row *matRowDef="let row; columns: colsForRendimentosPatrimonio"></tr>
</table>

And yes, I tried to call it in other code lines, but it didn't work. I also added the pipe to declarations of the app.module.ts . I don't understand what's happening. What I want to do is call that pipe given a string and an array of strings.

fjcn97
  • 11
  • 4
  • 7
    Welcome to SO. Please do not paste images of code, paste them as text instead. For more information, see [How to Ask](https://stackoverflow.com/help/how-to-ask) and take the [tour](https://stackoverflow.com/tour). – Nicholas K Dec 23 '19 at 16:01
  • Did you add the `pipe` to the providers of the module? – maury844 Dec 23 '19 at 16:08
  • I added the pipe to declarations of the app.module.ts . Info added on the post. – fjcn97 Dec 23 '19 at 16:37
  • 1
    I pasted code above. – fjcn97 Dec 26 '19 at 11:47
  • @fjcn97 welcome!. What you call "pipe" i guest that is called "filter", and you are right by stating that the way to pass aditional arguments to the filter function is using ":" colons. I use angularjs (v1) that is not angular (v6). If you show the code of the filter `showDateIntervenientValueForDesconhecido`, you will show the other part required to solve the problem. Also there is a lot of html snippets that aren't part of the problem. – Victor Dec 26 '19 at 16:46
  • @Victor In Angular 6, the pipe I show above is called a pure pipe, not filter. I added the code of the pipe showDateIntervenientValueForDesconhecido. – fjcn97 Dec 27 '19 at 11:15
  • have you added the code of the pipe (or filter?). I mean... i see a function called `transform` that looks like related to `showDateIntervenientValueForDesconhecido`, but nothing more, thus i'm not getting the full picture. I think that you are having some trouble defining filters... Tell me if this snippet helps a little, it shows how to define a customs filter (or customs pipes) in angulajs that also uses additional arguments... https://plnkr.co/edit/fAFEBfppynkb7ccaqcnc?p=preview – Victor Dec 27 '19 at 12:20
  • @Victor, I pasted the full code of the pipe. – fjcn97 Dec 27 '19 at 15:25
  • now this a step that helps other towards helping you to find the issue @fjcn97... i don't know angular 6... i'm sure other will, and will help you... i humble suggests you to follow this entry at the official docs: https://angular.io/guide/pipes#custom-pipes.. for sure it is a tiny detail – Victor Dec 27 '19 at 15:59
  • 1
    Yes, @Victor. It was a tiny detail... It was missing the return statement inside the pipe method transform. Thank you, for the help. – fjcn97 Dec 30 '19 at 11:36

1 Answers1

1

The documentation of the pipes helped me discovering the issue. It was a little detail - the return statement inside the pipe method transform was missing. So, the final code is this:

transform(dDataRecibo: string, monthlyYields: string[]): string {
    let i = 0;
    let bothAreNotZero = true;
    let yieldIntervenientValue = "Sem Rendimento";

    while (i < monthlyYields.length && bothAreNotZero) {
      if (monthlyYields[i]['nTitular'] === "0"
      && monthlyYields[i]['nAvalista'] === "0"
      && monthlyYields[i]['dDataRecibo'] === dDataRecibo) {
        yieldIntervenientValue = monthlyYields[i]['mRendimentoLiquidoMensal'];
        bothAreNotZero = false;
      }
      i++;
    }

    return yieldIntervenientValue;
}
fjcn97
  • 11
  • 4