简体   繁体   English

使用Firebase的Firestore的动态下拉菜单-AngularFire2

[英]Dynamic dropdowns using Firebase's Firestore - AngularFire2

I have created a collection like so (JSON equivalent): 我已经创建了一个这样的集合(相当于JSON):

sports: {
  football: {
    name: 'football',
    varieties: {
      11aside: {
        name: '11 a side'
      },
      6aside: {
        name: '6aside'
      }
    }
  }
}

I can display the different sports in a select box like so: 我可以在选择框中显示不同的运动,如下所示:

// sports.controller.ts
this.sports = db.collection('sports').valueChanges();

// sports.html
<select formControlName="sport">
    <option value="">- Select -</option>
    <option *ngFor="let sport of sports | async" [value]="sport.id">{{sport.name}}</option>
</select>

Once a selection has been made, I would like to display a 2nd select box, and let the user choose the 'variety'. 做出选择后,我想显示第二个选择框,然后让用户选择“品种”。 What would be the best way of achieving this in angularfire2? 在angularfire2中实现此目标的最佳方法是什么?

Thanks for any help. 谢谢你的帮助。

I can't see why this has to be dependent on angularfire2 in particular. 我不明白为什么这必须特别依赖于angularfire2。 Assuming you have initialized your formControl for first select dropdown sport as an empty string and on initial load it is not pre-selected with a valid option. 假设您已为首次选择下拉列表sport初始化了formControl为空字符串,并且在初始加载时未使用有效选项对其进行预选择。 Like, 喜欢,

this.form = this.formbuilder.group({
 sport: ''
});

You can look for its value change in the template to show the corresponding secondary dropdown for varieties. 您可以在模板中查找其值更改,以显示相应的品种下拉列表。

Assuming you have a 'property' to hold the value of varieties at any given instant based on the selected value, in your component listen for the first select control's value change, 假设您有一个“属性”可以根据选定的值在任何给定的瞬间保存品种的值,则在组件中侦听第一个选择控件的值更改,

varieties: any[] = null; // declare varieties property in component   

this.form.get('sport').valueChanges
  .subscribe((newVal) => {
    if (!newval) {
      this.varieties = null;
    } else {
      this.varieties = this.sports.filter((elem) => elem.id === newVal)[0].varieties;
      // assuming each newVal is actually the property value of 'id' based on '[value]="sport.id"' <-- this assignment in the template
    }
  });

Now simply add this new select dropdown as well to your template, 现在,只需将此新的select下拉列表也添加到您的模板中,

<select formControlName="sport">
    <option value="">- Select -</option>
    <option *ngFor="let sport of sports | async" [value]="sport.id">{{sport.name}}</option>
</select>
<select *ngIf="varities">
    <option value="">- Select -</option>
    <option *ngFor="let variety of varities" [value]="variety.name">{{variety.name}}</option>
</select>

You might have to modify the code based on the actual value to be assigned for this new select dropdown and might even add a new formControl for the varieties selection. 您可能必须根据要为此新选择下拉列表分配的实际value来修改代码,甚至可能为品种选择添加新的formControl。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM