[ngx-formly] Using field hooks to listening value changes
to value Using Field Hooks changes ngx
2023-09-14 08:59:14 时间
Every serious form in large apps has some dependent fields. For instance a dropdown field containing all nations and another dropdown field displaying the cities based on the currently selected nation. In this less we're looking into how we can dynamically load and filer our city dropdown based on the changes on our nation dropdown field.
import { Component } from '@angular/core'; import { FormGroup } from '@angular/forms'; import { FormlyFieldConfig } from '@ngx-formly/core'; import { DataService } from './core/data.service'; import { switchMap, startWith } from 'rxjs/operators'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], }) export class AppComponent { form: FormGroup = new FormGroup({}); model = { firstname: 'Zhentian', age: 31, nationId: 2, cityId: 1, }; fields: FormlyFieldConfig[] = [ { key: 'firstname', type: 'input', templateOptions: { type: 'text', label: 'FirstName', }, }, { key: 'age', type: 'input', templateOptions: { type: 'number', label: 'Age', }, }, { key: 'nationId', type: 'select', templateOptions: { label: 'Nation', options: this.dataService.getNations(), }, }, { key: 'cityId', type: 'select', templateOptions: { label: 'Cities', options: [], }, hooks: { onInit: (field: FormlyFieldConfig) => { field.templateOptions.options = field.form.get('nationId').valueChanges.pipe( startWith(this.model.nationId), switchMap(nationId => this.dataService.getCities(nationId)), ); }, }, }, ]; constructor(private dataService: DataService) {} onSubmit({ value, valid }) { console.log(value, valid); } }
相关文章
- new JSONObject(str)无法解析 报错:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
- java.lang.ClassCastException: org.apache.hadoop.io.Text cannot be cast to org.apache.h...
- LLVM Constant Value to c++ value
- [React Recoil] Write a Custom Recoil Hook to Reset a Value in the Recoil State
- [Typescript] Using Extract type until to get the value from Union type
- [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor
- [Angular] Adding keyboard events to our control value accessor component
- [Javascript] Adding Shapes to Maps with Leaflet and GeoJSON
- metadata request forward to GM6 - X-DevTools-Emulate-Network-Conditions-Cli
- 隐藏GridControl的“Drag a column header here to group by that column”
- 成功解决Value Error: Unable to add relationship because child variable ‘name‘ in ‘cats_df‘ is also its i
- 成功解决pandascoregeneric.py:3660: SettingWithCopyWarning: A value is trying to be set on a copy of a
- 已解决FutureWarning: The default value of regex will change from True to False in a future version. In
- 【错误记录】p7zip 交叉编译 Android 版本 NDK 报错 ( error: case value evaluates to -2 , which cannot be narrowed )
- Maven deploy 报错: Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:deploy (default-deploy) on project Failed to retrieve remote metadata /maven-metadata.xml
- How to terminating Sessions in Oracle
- iSpring SDK 全球领先 iSpring SDK PPT TO H5