0

I am showing some data using *ngFor I want to extract some value from *ngFor and show it for example in my heading. I tried to use {{ project }} but, it did not work. Every time it returned undefined but I am getting data inside ngOnInit()

My HTML looks like:

<div *ngIf="show; else noproject" class="container">
    <h1 class="mb-3 text-primary">Virtual machines for {{vms[project]}}</h1> 
    <button (click)="navigateToDetails(pid)" class="btn btn-success btn-sm mr-2"><i
        class="fas fa-plus-circle"></i>&nbsp;Add VM</button>
        <button (click)="navigateToDetails(pid)" class="btn btn-primary btn-sm mr-2"><i
          class="fas fa-plus-circle"></i>&nbsp;Commit Changes</button>
    <table class="table text-center table-hover table-striped">
      <thead appBtnd class="bg-primary text-white">
        <th appBtn [sortKey]="'name'" [data]="vms">VM Name</th>
        <th appBtn [sortKey]="'ipAddress'" [data]="vms">IP Address</th>
        <th appBtn [sortNumber]="'diskSize'" [data]="vms">Disk Size</th>
        <th appBtn [sortNumber]="'cpu'" [data]="vms">CPU</th>
        <th appBtn [sortNumber]="'ram'" [data]="vms">Ram</th>          
        <th>Gateway</th>
        <th>NetMask</th>
        <th>Actions</th>
      </thead>
      <tbody>
        <tr *ngFor="let vm of vms">
          <td>{{vm.name | titlecase }}</td>
          <td>{{vm.ipAddress}}</td>
          <td>{{vm.diskSize}}</td>
          <td>{{vm.cpu}}</td>
          <td>{{vm.ram}}</td>            
          <td>{{vm.gateway}}</td>
          <td>{{vm.netmask}}</td>
          <td>
            <button class="btn btn-sm btn-danger" type="submit" (click)="deleteVm(vm.id)">
              <i class="fas fa-trash"></i>
            </button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>    

Angular code:

export class VmForProjectComponent implements OnInit {
        vms: ProjectForUser show: boolean; pid: number; constructor(private alertify: AlertifyService, private route: ActivatedRoute, private vmService: VmService, private projectService: ProjectService, private router: Router) { } 
        ngOnInit() { 
            const projectId = +this.route.snapshot.paramMap.get('projectId'); 
            this.projectService.getVmByProjectId(projectId).subscribe(p => this.vms = p);
             if (this.vms !== null || undefined) { this.show = true; } 
        }

enter image description here

Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37
Arzu Suleymanov
  • 671
  • 2
  • 11
  • 33

1 Answers1

0

You may be able to create a view child element ref and set the value on ngAfterViewInit. Please see code below:

Template:

<input type="hidden" name="cpuTitleName"  #cpuTitle value="vm.project" />
   <button class="btn btn-sm btn-danger" type="submit" (click)="deleteVm(vm.id)">
      i class="fas fa-trash"></i>
   </button>

TS:

   export class AppComponent implements OnInit, AfterViewInit  {...
    @ViewChild('cpuTitle', {static: false}) projectEl: ElementRef;

    ngAfterViewInit(): void {
    this.cpuTitle = this.projectEl.nativeElement.value;
}

Full stack blitz:

https://stackblitz.com/edit/angular-mebigy?file=src%2Fapp%2Fapp.component.html

Paul
  • 460
  • 2
  • 7