【问题标题】:Angular 2 how to display the array object inside an array of objectsAngular 2如何在对象数组中显示数组对象
【发布时间】:2019-02-27 14:20:28
【问题描述】:

这是我的 json 文件,

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

在前端,因为 *ngFor 是这样给出的,

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

我的打字稿,

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;
    });
  }

我只能获取和显示姓名,无法获取scholardetails country_id、s奖学金名和奖学金名。我找不到我在哪里做错了。如果有人知道,请帮助我。

【问题讨论】:

    标签: node.js angular mongodb


    【解决方案1】:

    *ngFor 中的 country 变量只能在其对应的 div 标记内访问,因此您的前端 HTML 需要如下所示:

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

    【讨论】:

      【解决方案2】:

      您需要将第二个 ngFor 放在第一个 ngFor 中并有一个嵌套的 ngFor

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

      【讨论】:

      • 看不懂,能简单说一下吗?
      • 您需要将第二个 ngfor 放在第一个 div 中
      【解决方案3】:

      试试这个,第二个ng for应该是这样的。

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

      【讨论】:

        猜你喜欢
        • 2017-04-07
        • 1970-01-01
        • 2017-09-20
        • 2017-05-01
        • 1970-01-01
        • 2018-09-24
        • 2022-07-07
        • 1970-01-01
        • 2019-05-07
        相关资源
        最近更新 更多