Search code examples
angularngfor

*ngFor values from different variables


I need show inside ngFor values than are in another variable, i don't know if this is possible,

now i have this

Component

import { Component, OnInit, OnDestroy } from '@angular/core';
import { ListarAfiliacaoService } from '../services/listar.afiliacao.service';
import { MatDialog } from '@angular/material/dialog';
import { ListarImovelService } from '../services/listar.imovel.service';
import { Observable } from 'rxjs';

@Component({
  selector: 'app-afiliacao',
  templateUrl: './afiliacao.component.html',
  styleUrls: ['./afiliacao.component.css']
})
export class AfiliacaoComponent implements OnInit, OnDestroy {

  constructor(private listAffiliationService: ListarAfiliacaoService, public dialog: MatDialog) { }

  affiliation$: Observable<any>;
  affiliationPending$: Observable<any>;
  afiliationDenied: any;
  accessCount: any;


  ngOnInit(): void {
    this.affiliation$ = this.listAffiliationService.listAffiliation();
    this.affiliationPending$ = this.listAffiliationService.listAffiliationPending();
    this.listAffiliationDenied();
  }

  countAccess(refValue) {
    this.listAffiliationService.countAccess(refValue).subscribe(data => {
      this.accessCount = data.length;   
    }, err => {
      console.log(err)
    }) 
  }

  listAffiliationDenied() {
    this.listAffiliationService.listAffiliationDenied().subscribe(data => {
      this.afiliationDenied = data;    
    }, err => {
      console.log(err)
    })  
   }

  ngOnDestroy(): void {
  
  }
}

Service

import { Injectable } from '@angular/core';
import { HttpClient , HttpHeaders, HttpResponse } from '@angular/common/http';
import { AuthService } from './auth.service';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ListarAfiliacaoService {

  constructor(private http: HttpClient, private authService: AuthService) { }

  listAffiliation(): Observable<any> {
    this.authService.loadToken();
    let reqheaders = new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + this.authService.authToken
    });
    return this.http.get('http://192.168.15.64:4000/afiliacao/'+this.authService.user_id, {headers: reqheaders})
  }

  listAffiliationPending(): Observable<any> {
    this.authService.loadToken();
    let reqheaders = new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + this.authService.authToken
    });
    return this.http.get('http://192.168.15.64:4000/afiliacao/pedido/'+this.authService.user_id, {headers: reqheaders})
  }

  listAffiliationDenied(): Observable<any> {
    this.authService.loadToken();
    let reqheaders = new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + this.authService.authToken
    });
    return this.http.get('http://192.168.15.64:4000/afiliacao/negado/'+this.authService.user_id, {headers: reqheaders})
  }

  countAccess(ref): Observable<any> {
    this.authService.loadToken();
    let reqheaders = new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': 'Bearer ' + this.authService.authToken
    });
    return this.http.get('http://localhost:4000/count/'+ref, {headers: reqheaders})
  }
}

HTML

<table class="table table-hover">
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Total access count</th>
      <th scope="col">Movel ID</th>
      <th scope="col">Status</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
      <tr class="table-success" *ngFor="let afilia of affiliation$ | async">
          <th scope="row">{{afilia.ref}}</th>
          <td>{{}}</td>
          <td>{{afilia.imovel_Id}}</td>
          <td>{{afilia.status}}</td>
          <td><mat-icon aria-hidden="false" aria-label="Example home icon">info</mat-icon></td>
        </tr>
  </tbody>
</table>

I don't know what i put to do this "Total access count", i created functions but can't show that. This refValue is afilia.ref inside HTML.

I put allthe service code and Component


Solution

  • I think you can use another async pipe in your template, like this:

    <tr class="table-success" *ngFor="let afilia of affiliation$ | async">
        <th scope="row">{{ afilia.ref }}</th>
        <td>{{ countAccess(afilia.ref) | async }}</td>
        <td>{{ afilia.imovel_Id }}</td>
        <td>{{ afilia.status }}</td>
        <td><mat-icon aria-hidden="false" aria-label="Example home icon">info</mat-icon></td>
    </tr>
    

    You would need to modify countAccess() in controller as follows:

    countAccess(refValue) {
      return this.listAffiliationService.countAccess(refValue)
        .pipe(
          map(data => data.length) 
        );
    }
    

    But it may be better to construct your stream in the controller.