1

This is my json file,

            country_id:String,
            name:String
            scholardetails:[{
                country_id:String,
                scholarshipname:String,
                scholarshiplink:String
            }]
        },{collection:'new_countryMaster'});

In frontend, for *ngFor gave like this,

 <div *ngFor="let country of countries">
       <p>{{country.name}}<p>
  </div>
<div *ngFor="let item of countries.scholardetails">
       <p>{{item.scholarshipname}}<p>
  </div>

my typescript,

countries: any;

  constructor(private route: ActivatedRoute, private http: HttpClient) { }

  ngOnInit() {
    this.getscholardetails(this.route.snapshot.params['id']);
  }

  getscholardetails(id) {
    this.http.get('http://localhost:3000/api/scholardetails/'+id).subscribe(data => {
      this.countries= data;
    });
  }

I only can get and display name and not be able to get scholardetails country_id, scholarshipname and scholarshipname. I can't find where i did mistake.please help me if anyone know.

3 Answers 3

2

The country variable in your *ngFor is only accessible inside it's corresponding div tag, so your frontend HTML needs to be like this:

<div *ngFor="let country of countries">
   <p>{{country.name}}<p>

   <div *ngFor="let item of country.scholardetails">
      <p>{{item.scholarshipname}}<p>
   </div>

 </div>
Sign up to request clarification or add additional context in comments.

Comments

1

You need to place the second ngFor inside the first one and have a nested ngFor

<div *ngFor="let country of countries"> <p>{{country.name}}<p>
    <div *ngFor="let item of countries.scholardetails">     
<p>{{item.scholarshipname}}<p>
 </div>
</div> 

2 Comments

i can't understand,can you tell briefly?
You need to place the second ngfor within the first div
0

Try this, The second ng for should be like this.

<div *ngFor="let item of countries['scholardetails']"> {{item.scholarshipname}}
</div> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.