0

I have a simple Angular app (Angular: 15.1.4) running in Azure using Static Web App and Azure Functions as the API. The app currently gets all records and displays them in a table using ngFor however i have a "view detail" component using a ngIf that doesnt display anything.

The API is returning the data correctly in JSON format however its not being displayed on the page:

Network Trace from Inspect

component.html

<div *ngIf="haction">
  <h2>{{haction.serverName | uppercase}} Details</h2>
  <div><span>id: </span>{{haction.id}}</div>  
  <button type="button" (click)="goBack()">go back</button>
  <button type="button" (click)="save()">save</button>
</div>`

component.ts

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { Haction } from '../haction';
import { HactionService } from '../haction.service';

@Component({
  selector: 'app-haction-detail',
  templateUrl: './haction-detail.component.html',
  styleUrls: ['./haction-detail.component.css']
})
export class HactionDetailComponent implements OnInit {
  haction: Haction | undefined;

  constructor(
    private route: ActivatedRoute,
    private hactionService: HactionService,
    private location: Location
  ) {}

  ngOnInit(): void {
    this.getHaction();
  }

  getHaction(): void {
    const id = Number(this.route.snapshot.paramMap.get('id'));
    this.hactionService.getHaction(id)
      .subscribe(haction => this.haction = haction);
  }


  goBack(): void {
    this.location.back();
  }

  save(): void {
    if (this.haction) {
      this.hactionService.updateHaction(this.haction)
        .subscribe(() => this.goBack());
    }
  }
}

service.ts

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { catchError, map, tap } from 'rxjs/operators';
import { Haction } from './haction';

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

  private getHactionsUrl = 'api/getHiveActions';  // URL to web api
  private getHactionUrl = 'api/getHiveAction';  // URL to web api

  httpOptions = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  };

  constructor(
    private http: HttpClient,
    private messageService: MessageService) { }

  /** GET Hive Actions from the server */
  getHactions(): Observable<Haction[]> {
    return this.http.get<Haction[]>(this.getHactionsUrl)
      .pipe(
        tap(_ => this.log('fetched hive actions')),
        catchError(this.handleError<Haction[]>('getHactions', []))
      );
  }


  /** GET hero by id. Will 404 if id not found */
  getHaction (id: number): Observable<Haction> {
    const url = `${this.getHactionUrl}?id=${id}`;
    return this.http.get<Haction>(url).pipe(
      tap(_ => this.log(`fetched haction id=${id}`)),
      catchError(this.handleError<Haction>(`getHaction id=${id}`))
    );
  }
}`
Matt Auer
  • 1
  • 2
  • Your call is async, data not available when page loads. Check out the bit in this post about ASYNC [Properly use Async calls with Angular 5](https://stackoverflow.com/questions/48098288/properly-use-async-calls-with-angular-5) –  Mar 02 '23 at 11:16
  • Thank you for the response, i havent been able to get the async working in the html and the example in the link was for ngFor: If i add | async it errors when compiling `

    {{haction.serverName | uppercase}} Details

    id: {{haction.id}}
    ` Error: src/app/haction-detail/haction-detail.component.html:3:35 - error TS2532: Object is possibly 'undefined'. 3
    id: {{haction.id}}
    src/app/haction-detail/haction-detail.component.ts:11:16 11 templateUrl: './haction-detail.component.html',
    – Matt Auer Mar 02 '23 at 11:40
  • And then there is google.... https://www.google.com/search?q=async+with+ngIf&oq=async+with+ngIf –  Mar 02 '23 at 11:46
  • https://gist.github.com/jhades/a6fdf54a18dc4e539226818ee3482020#file-11-ts –  Mar 02 '23 at 11:48

0 Answers0