Help a newbie? I’ve got an ion-select working great getting it’s ion-select-option values and text from Firebase. But I can’t seem to feed it a default with patchValue. If I use a static array of values it works. If I use a static default value with a dynamic ion-select it does not work.
Using Firebase Firestore, Ionic 5, latest Angular, latest AngularFire, latest Chrome, and latest Capacitor.
MAY BE IMPORTANT: When I click the select to drop down, I see the default value appear. This is before I click any values. And it appears on top of the label, so it doesn’t look right. I think ionChange is firing though, because I inserted a console.log() into my onCoopChange() method and it shows it is being called on page load, with an null value on this.selectedCoop.
I believe the id is indexing correctly because if I change the 0 on this line to 1, it indexes the second item in the collection. I see the default value show up when I click the down arrow, overlaid over the label.
this.selectedCoop = coops[0].id;
This is the database schema:
Broken code, TS:
import { Component, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreDocument, AngularFirestoreCollection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { FormGroup, FormControl } from '@angular/forms';
export interface Command { command: string; }
export interface Coop {
id: number;
name: string;
}
@Component({
selector: 'app-status',
templateUrl: './status.page.html',
styleUrls: ['./status.page.scss'],
})
export class StatusPage implements OnInit {
coopSelectorForm: FormGroup;
private coopCollection: AngularFirestoreCollection<Coop>;
coops: Observable<Coop[]>;
selectedCoop: number;
constructor(private afs: AngularFirestore) {
this.coopCollection = afs.collection<Coop>(
'coops', ref => ref.orderBy('name'));
this.coops = this.coopCollection.valueChanges();
this.coopSelectorForm = new FormGroup({ selectedCoop: new FormControl(), });
this.coops.subscribe(coops => {
this.selectedCoop = coops[0].id;
this.coopSelectorForm.controls['selectedCoop'].patchValue(this.selectedCoop);
});
}
ngOnInit() {
}
onCoopChange(value) {
this.selectedCoop = value.selectedCoop;
}
Broken code, HTML:
<ion-header>
<ion-toolbar color="primary">
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
</ion-buttons>
<ion-title>
Status
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<form [formGroup]="coopSelectorForm">
<ion-list>
<ion-item class="input-item">
<ion-label position="floating">Coop</ion-label>
<ion-select interface="popover" formControlName="selectedCoop"
(ionChange)="onCoopChange(coopSelectorForm.value)">
<ion-select-option *ngFor="let coop of coops | async" [value]="coop.id">
{{coop.name}}
</ion-select-option>
</ion-select>
</ion-item>
</ion-list>
</form>
</ion-content>
Working static default value, static array, TS:
import { Component, OnInit } from '@angular/core';
import { AngularFirestore, AngularFirestoreDocument, AngularFirestoreCollection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { FormGroup, FormControl } from '@angular/forms';
export interface Command { command: string; }
export interface Coop {
id: number;
name: string;
}
@Component({
selector: 'app-status',
templateUrl: './status.page.html',
styleUrls: ['./status.page.scss'],
})
export class StatusPage implements OnInit {
coopSelectorForm: FormGroup;
private coopCollection: AngularFirestoreCollection<Coop>;
coops: Array<Coop>;
selectedCoop: number;
constructor(private afs: AngularFirestore) {
this.coopCollection = afs.collection<Coop>(
'coops', ref => ref.orderBy('name'));
this.coops = [{ id: 1, name: 'First' }, { id: 2, name: 'Second' }, { id: 3, name: 'Third' }];
this.coopSelectorForm = new FormGroup({ selectedCoop: new FormControl(), });
this.selectedCoop = 1;
this.coopSelectorForm.patchValue({ selectedCoop: this.selectedCoop });
}
ngOnInit() {
}
onCoopChange(value) {
this.selectedCoop = value.selectedCoop;
}
Working static default value, static array HTML:
<ion-header>
<ion-toolbar color="primary">
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
</ion-buttons>
<ion-title>
Status
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<form [formGroup]="coopSelectorForm">
<ion-list>
<ion-item class="input-item">
<ion-label position="floating">Coop</ion-label>
<ion-select interface="popover" formControlName="selectedCoop"
(ionChange)="onCoopChange(coopSelectorForm.value)">
<ion-select-option *ngFor="let coop of coops" [value]="coop.id">
{{coop.name}}
</ion-select-option>
</ion-select>
</ion-item>
</ion-list>
</form>
</ion-content>




