Angular 10 - Formly - 在自定义 formly-wrapper 中访问字段的值
创始人
2024-10-15 06:00:41
0

在Angular 10中,你可以使用Formly来创建动态表单。当你需要在自定义formly-wrapper中访问字段的值时,可以使用以下解决方法:

  1. 创建一个自定义的formly-wrapper组件,例如CustomWrapperComponent:
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { FieldWrapper } from '@ngx-formly/core';

@Component({
  selector: 'app-custom-wrapper',
  template: `
    
`, }) export class CustomWrapperComponent extends FieldWrapper implements AfterViewInit { @ViewChild('fieldComponent', { static: true }) fieldComponent; ngAfterViewInit() { // 在视图初始化之后,可以访问字段的值 console.log(this.fieldComponent.field.formControl.value); } }
  1. 在你的表单配置中使用自定义的formly-wrapper组件:
import { Component } from '@angular/core';
import { FormlyFieldConfig } from '@ngx-formly/core';

@Component({
  selector: 'app-root',
  template: `
    
`, }) export class AppComponent { form = new FormGroup({}); model = {}; fields: FormlyFieldConfig[] = [ { key: 'name', type: 'input', templateOptions: { label: 'Name', required: true, }, wrappers: ['custom-wrapper'], }, ]; onSubmit() { console.log(this.model); } }
  1. 在你的模块中声明和导入自定义的formly-wrapper组件:
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { FormlyModule } from '@ngx-formly/core';
import { FormlyBootstrapModule } from '@ngx-formly/bootstrap';

import { AppComponent } from './app.component';
import { CustomWrapperComponent } from './custom-wrapper.component';

@NgModule({
  declarations: [AppComponent, CustomWrapperComponent],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    FormlyModule.forRoot(),
    FormlyBootstrapModule,
  ],
  bootstrap: [AppComponent],
})
export class AppModule {}

通过以上步骤,你可以在自定义的formly-wrapper组件中访问字段的值。

相关内容

热门资讯

透视能赢!hhpoker有后台... 透视能赢!hhpoker有后台操作吗(透视)一直有挂(详细辅助第三方教程)1、在ai机器人技巧中,中...
攻略讲解!wepoker辅助器... 攻略讲解!wepoker辅助器安装包定制,hhpoker到底可以作弊吗,必备教程(有挂透视)1、不需...
透视好友!德州来玩辅助器(透视... 透视好友!德州来玩辅助器(透视)原来真的有挂(详细辅助安装教程)1、实时开挂更新:用户可以随时随地访...
实测交流!wepokerplu... 实测交流!wepokerplus辅助,hhpoker有透视功能吗,德州教程(有挂方法);支持2-10...
透视好友房!hhpoker辅助... 透视好友房!hhpoker辅助挂是真的吗(透视)一直是有挂(详细辅助黑科技教程);1、不需要AI权限...
最新研发!hhpoker必备开... 最新研发!hhpoker必备开挂,wepoker有透视吗,揭秘攻略(有挂教程);原来确实真的有挂(需...
透视辅助!wepokerplu... 透视辅助!wepokerplus作弊(透视)原来真的有挂(详细辅助细节方法)1、构建自己的辅助插件;...
玩家必用!佛手大菠萝有挂吗,德... 玩家必用!佛手大菠萝有挂吗,德普之星有辅助软件吗,AI教程(有挂教程)这是由厦门游乐互动科技有限公司...
透视了解!hhpoker可以控... 透视了解!hhpoker可以控制吗(透视)本来真的是有挂(详细辅助普及教程);小薇(透视辅助)致您一...
交流学习经验!sohoo开挂辅... 交流学习经验!sohoo开挂辅助,wepoker脚本,详细教程(有挂方法)1、不需要AI权限,帮助你...