I have a web api (.NET Core 3.1) that returns a json like the following:
[
{
"counterparty": "Santander",
"tradeDate": "2020-05-23T10:03:12",
"isin": "DOL110",
"typology": 0
},
{
"counterparty": "Jordan Banks",
"tradeDate": "2020-06-11T11:23:22",
"isin": "LIT250",
"typology": 0
},
{
"counterparty": "Santander",
"tradeDate": "2020-06-11T11:24:08",
"isin": "LIT300",
"typology": 0
}
]
I consume this web api with the component and angular service below. So far, I return the counterparty field of all objects.
operations.component.ts:
import { Component, OnInit } from '@angular/core';
import { OperationsService } from "./operations.service";
@Component({
selector: 'app-operations',
templateUrl: './operations.component.html',
styleUrls: ['./operations.component.css']
})
export class OperationsComponent implements OnInit {
data: any;
constructor(private operationsService: OperationsService) { }
ngOnInit(): void {
this.loadOperations();
}
loadOperations() {
return this.operationsService.getOperations().subscribe(source => this.data = source);
}
}
operations.component.html:
<div *ngFor="let item of data">
<div>{{item.counterparty}}</div>
</div>
operations.service.ts:
import { Injectable, Inject } from "@angular/core";
import { HttpClient } from "@angular/common/http";
@Injectable({
providedIn: "root"
})
export class OperationsService {
constructor(private http: HttpClient) { }
public getOperations() {
return this.http.get("https://localhost:44329/api/operations");
}
}
app.module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { NavMenuComponent } from './nav-menu/nav-menu.component';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './user/login/login.component';
import { OperationsComponent } from './operations/operations/operations.component';
@NgModule({
declarations: [
AppComponent,
NavMenuComponent,
HomeComponent,
LoginComponent,
OperationsComponent
],
imports: [
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
HttpClientModule,
FormsModule,
RouterModule.forRoot([
{ path: '', component: HomeComponent, pathMatch: 'full' },
{ path: 'api/operations', component: OperationsComponent }
])
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
what i need now is to apply a filter in the counterparty field to return only the distinct values, that is, without repetitions of equal values. I'm trying with ng-repeat, but i have the error:
Uncaught Error: Template parse errors: The pipe 'unique' could not be found ("]item of data | unique: item.counterparty"> {{item.counterparty}} ")
So, how can I get the distinct values of the array? Can I do it only in component.html or do I also have to change component.ts?