【发布时间】:2019-03-13 00:27:10
【问题描述】:
我有 3 个文件:
- 从 Firestore 收集我的数据并构建表单的服务
- component.ts 文件
- component.html 文件
对于我的大部分输入,我的表单都能正确读取来自服务/表单构建器的数据。我遇到困难的区域是使用 Angular Material Select (dropdown) sn-p。
在我的 component.ts 文件中,我有一个包含两个选项的数组供用户选择。选择后,该选项将作为字符串保存到 firestore。
正如您在下面看到的,目前我的 HTML 正在从其关联的 component.ts 文件中的数组中读取。理想情况下,我希望它显示来自 firestore 的当前值,并在单击它时提供两个可用选项。
如何在 HTML 中提供这两个选项以及显示来自 firestore 的当前设置值?
服务
export interface IUserSettings {
usersetting_dateformat: string;
}
@Injectable()
export class SettingsService {
// Database
private settingsDocRef: AngularFirestoreDocument<any>;
settingsDocument: Observable<any>;
// User
userID: string;
// Form
editForm = new FormGroup({});
// User Settings Behaviour Subject
userSettings$: BehaviorSubject<IUserSettings>;
constructor(
private readonly afs: AngularFirestore, fb: FormBuilder) {
this.editForm = fb.group({
usersetting_dateformat: [''],
});
this.userSettings$ = new BehaviorSubject({
dateformat: 'DD/MM/YYYY',
});
}
getSettingsData() {
this.settingsDocRef = this.afs.doc(`settings/${this.userID}`);
this.settingsDocument = this.settingsDocRef.snapshotChanges();
this.settingsDocument.subscribe(value => {
const userSettings = value.payload.data() as IUserSettings;
this.userSettings$.next(userSettings);
this.editForm.patchValue(this.userSettings$.getValue());
});
}
}
Component.ts
...
dateFormats = ["DD/MM/YYYY", "MM/DD/YYYY"];
...
Component.html
<form [formGroup]="settingsService.editForm">
<mat-select placeholder="Date Format">
<mat-option *ngFor="let dateFormat of dateFormats" [value]="dateFormat"> {{dateFormat}} </mat-option>
</mat-select>
</form>
【问题讨论】:
标签: arrays angular google-cloud-firestore angular-reactive-forms