Home > OS >  ngFor directive is not rendering my div or its data
ngFor directive is not rendering my div or its data

Time:12-03

I have created a service to which i am subscribed to in my component, however, the div is not rendering at all when i use ngFor.

Component.TS file

 import { Component, OnInit } from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
import { PlansService } from 'src/app/services/plans.service';


interface JobType {
  value: string;
  viewValue: string;
}

interface WorkEnv {
  value: string;
  viewValue: string;
}


@Component({
  selector: 'app-post-job',
  templateUrl: './post-job.component.html',
  styleUrls: ['./post-job.component.css']
})

export class PostJobComponent implements OnInit {

  jobTypes: JobType[] = [
    {value: 'type-0', viewValue: 'Full-Time'},
    {value: 'type-1', viewValue: 'Part-Time'},
    {value: 'type-2', viewValue: 'Freelance'},
 
  ];

  workEnvs: WorkEnv[] = [
    {value: 'type-0', viewValue: 'Remote'},
    {value: 'type-1', viewValue: 'Hybrid'},
    {value: 'type-2', viewValue: 'On-site'},
 
  ];

  jobForm = this.fb.group({ 

    jobTitle: this.fb.group({
      title: ['', [Validators.required, Validators.minLength(2), Validators.pattern('^[_A-z0-9]*((-|\s)*[_A-z0-9])*$')]],
      jobType: ['', [Validators.required]]
    }),

   })

   plans: any;
  
  constructor(private fb: FormBuilder, private service:PlansService) {
   }

  ngOnInit(): void {

    this.service.getPlans()
      .subscribe(response => {
        this.plans = response;
        console.log(response);
      });
    
  }



   // Getter method to access formcontrols
   get myForm() {
    return this.jobForm.controls;
  }

 
  // Submit Registration Form
  // onSubmit() {
  //   this.submitted = true;
  //   if(!this.jobForm.valid) {
  //     alert('Please fill all the required fields to create a super hero!')
      
  //   } else {
  //     console.log(this.jobForm.value)
  //   }
  // }

}

Component.html file

<div  >
  <div  *ngFor="let plan of plans " >
      <h1>{{plan.title}}</h1>
  </div>
</div>

the data is showing in my console.log but it is not rendering on the card at all. Please help, thank you in advance!

I tried to add a dummy array in the ts file and i printed that to the console log but still did not render in the div

CodePudding user response:

You can load your plans async.

  1. Create an observable public plans$: Observable<any>

  2. Assign your subscription this.plans$ = this.service.getPlans();

  3. In your HTML render conditionally

<ng-container *ngIf="plans$ | async as plans">
 <div *ngFor="let plan of plans">
  {{ plan | json }} 
 </div>
</ng-container>

CodePudding user response:

Use Change detection strategy in angular :

@Component({
  selector: 'app-post-job',
  templateUrl: './post-job.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  styleUrls: ['./post-job.component.css']
})


constructor(private ref: ChangeDetectorRef) {
 
}


 ngOnInit(): void {

this.service.getPlans()
  .subscribe(response => {
    this.plans = response;
    console.log(response);
    this.ref.detectChanges(); // manually trigger change detection
  });

}

  • Related