Angular 2 - 组件内的 formControlName

2024-03-19

我想创建一个可以与 FormBuilder API 一起使用的自定义输入组件。我该如何添加formControlName在组件内部?

模板:

<label class="custom-input__label"
          *ngIf="label">
        {{ label }}
</label>
<input class="custom-input__input" 
       placeholder="{{ placeholder }}"
       name="title" />
<span class="custom-input__message" 
      *ngIf="message">
        {{ message }}
</span>

成分:

import {
    Component,
    Input,
    ViewEncapsulation
} from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'custom-input',
    host: {
        '[class.custom-input]': 'true'
    },
    templateUrl: 'input.component.html',
    styleUrls: ['input.component.css'],
    encapsulation: ViewEncapsulation.None,
})
export class InputComponent {
    @Input() label: string;
    @Input() message: string;
    @Input() placeholder: string;
}

Usage:

<custom-input label="Title" 
           formControlName="title" // Pass this to input inside the component>
</custom-input>

你不应该添加formControlName属性到自定义组件模板中的输入字段。 您应该添加formControlName根据最佳实践,在自定义输入元素本身上。

您可以在自定义输入组件中使用的是controlValueAccessor每当您的自定义输入模板中的输入字段发生更改或模糊事件时,您的自定义输入都会更新值。

它在自定义输入的表单控件行为和为该自定义表单控件提供的 UI 之间提供连接(以更新值或其他需求)。

下面是 TypeScript 中自定义输入组件的代码。

import { Component, Input, forwardRef, AfterViewInit, trigger, state, animate, transition, style, HostListener, OnChanges, ViewEncapsulation, ViewChild, ElementRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor, FormControl } from '@angular/forms';

export const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => InputComponent),
    multi: true
};

@Component({
  selector: 'inv-input',
  templateUrl:'./input-text.component.html',
    styleUrls: ['./input-text.component.css'],
    encapsulation: ViewEncapsulation.None,
  providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR],
    animations:[trigger(
        'visibilityChanged',[
            state('true',style({'height':'*','padding-top':'4px'})),
            state('false',style({height:'0px','padding-top':'0px'})),
            transition('*=>*',animate('200ms'))
        ]
    )]
})

export class InputComponent implements ControlValueAccessor, AfterViewInit, OnChanges {

    // Input field type eg:text,password
    @Input()  type = "text"; 

    // ID attribute for the field and for attribute for the label
    @Input()  idd = ""; 

    // The field name text . used to set placeholder also if no pH (placeholder) input is given
    @Input()  text = ""; 

    // placeholder input
    @Input()  pH:string; 

    //current form control input. helpful in validating and accessing form control
    @Input() c:FormControl = new FormControl(); 

    // set true if we need not show the asterisk in red color
    @Input() optional : boolean = false;

    //@Input() v:boolean = true; // validation input. if false we will not show error message.

    // errors for the form control will be stored in this array
    errors:Array<any> = ['This field is required']; 

    // get reference to the input element
    @ViewChild('input')  inputRef:ElementRef; 


    constructor() {

    }

    ngOnChanges(){

    }

    //Lifecycle hook. angular.io for more info
    ngAfterViewInit(){ 
        // set placeholder default value when no input given to pH property      
        if(this.pH === undefined){
            this.pH = "Enter "+this.text; 
        }

        // RESET the custom input form control UI when the form control is RESET
        this.c.valueChanges.subscribe(
            () => {
                // check condition if the form control is RESET
                if (this.c.value == "" || this.c.value == null || this.c.value == undefined) {
                    this.innerValue = "";      
                    this.inputRef.nativeElement.value = "";                 
                }
            }
        );
    }

   //The internal data model for form control value access
    private innerValue: any = '';

    // event fired when input value is changed . later propagated up to the form control using the custom value accessor interface
    onChange(e:Event, value:any){
        //set changed value
        this.innerValue = value;
        // propagate value into form control using control value accessor interface
        this.propagateChange(this.innerValue);

        //reset errors 
        this.errors = [];
        //setting, resetting error messages into an array (to loop) and adding the validation messages to show below the field area
        for (var key in this.c.errors) {
            if (this.c.errors.hasOwnProperty(key)) {
                if(key === "required"){
                    this.errors.push("This field is required");
                }else{
                    this.errors.push(this.c.errors[key]);
                }              
            }
        }
    }



    //get accessor
    get value(): any {
        return this.innerValue;
    };

    //set accessor including call the onchange callback
    set value(v: any) {
        if (v !== this.innerValue) {
            this.innerValue = v;
        }
    }

    //propagate changes into the custom form control
    propagateChange = (_: any) => { }

    //From ControlValueAccessor interface
    writeValue(value: any) {
        this.innerValue = value;
    }

    //From ControlValueAccessor interface
    registerOnChange(fn: any) {
        this.propagateChange = fn;
    }

    //From ControlValueAccessor interface
    registerOnTouched(fn: any) {

    }
}

下面是自定义输入组件的 HTML 模板

<div class="fg">
      <!--Label text-->
      <label [attr.for]="idd">{{text}}<sup *ngIf="!optional">*</sup></label>
      <!--Input form control element with on change event listener helpful to propagate changes -->
      <input type="{{type}}" #input id="{{idd}}" placeholder="{{pH}}" (blur)="onChange($event, input.value)">
      <!--Loop through errors-->
      <div style="height:0px;" [@visibilityChanged]="!c.pristine && !c.valid" class="error">
            <p *ngFor="let error of errors">{{error}}</p>
      </div>
</div>

下面是自定义输入组件,可以在 fromGroup 中使用或单独使用

<inv-input formControlName="title" [c]="newQueryForm.controls.title" [optional]="true" idd="title" placeholder="Type Title to search"
          text="Title"></inv-input>

以这种方式,如果您实现自定义表单控件,您可以轻松应用自定义验证器指令并在该表单控件上累积错误以显示错误。

人们可以模仿相同的风格以上述方式开发自定义选择组件、单选按钮组、复选框、文本区域、文件上传等,并根据表单控件的行为要求进行细微的更改。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Angular 2 - 组件内的 formControlName 的相关文章

随机推荐

  • 如何从字典中随机选择一个项目?

    我是一个 python 初学者 试图制作二十一点游戏 并且一直不断收到有关此代码的多个关键错误 def rank rank rank 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 Jack 10 King 10
  • 使用新的 Spring UriComponentsBuilder 进行 URL 编码

    我正在尝试使用 spring 的 UriComponentsBuilder 来生成一些用于 oauth 交互的 url 查询参数包括回调url 带空格的参数值等实体 尝试使用 UriComponentBuilder 因为 UriUtils
  • 如何创建关联矩阵

    我正在尝试创建 1 1 1 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 1 0 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 S 1
  • 如何将RequiredFieldValidator添加到DropDownList控件?

    我有一个DropDownList与一个绑定SqlDataSource显示数据库中的值 我无法使用RequiredFieldValidator 大多数情况下 您将其视为正在验证任何其他类型的控件 但使用所需字段验证器的 InitialValu
  • 在 Swift 中显示当前位置并更新 MKMapView 中的位置

    我正在学习如何使用新的 Swift 语言 只有 Swift 没有 Objective C 为此 我想用地图做一个简单的视图 MKMapView 我想查找并更新用户的位置 就像在 Apple 地图应用程序中一样 我尝试了这个 但什么也没发生
  • 如何从链接到 QTableView 的模型中插入和删除行

    The removeRows 通过删除选定的行来按预期工作 但有一个问题insertRows 由于某种原因 新项目不会出现在所选索引号处 是什么原因导致这个问题呢 from PyQt4 QtCore import from PyQt4 Qt
  • Matplotlib:如何设置当前图形?

    希望这是一个简单的问题 但我目前无法弄清楚 我想使用 matplotlib 显示 2 个数字 然后交互式地使用它们 我用以下方法创建数字 import matplotlib import pylab as pl f1 pl figure f
  • Heroku 零停机时间

    是否可以做类似的事情Github 零停机部署 https github com blog 517 unicorn在 Heroku 上使用 Cedar 堆栈上的 Unicorn 我不完全确定 Heroku 上的重启是如何进行的 以及我们对重启
  • 从我的本地计算机附加文件以在 cq/AEM 中发送邮件

    我正在学习 AEM 并且正在满足一个要求 其中我能够发送电子邮件 但无法添加从我的计算机浏览的附件 要求 有一个用 HTML 制作的表单 可以从其中收集信息 并且有一个浏览按钮 可以从其中上传文件 文件上传后 应立即将包含表单内容和附件的电
  • 使用 selenium webdriver 按类名和标记名查找元素

    有类和标记名 我正在编写下面的硒代码以从下面的代码中查找描述 但它不起作用 WebElement WWdescription driver findElement By className atb delivery accordions c
  • Swift:如何在“String”扩展中添加类方法

    我想在扩展中添加一个类函数 extension String class func test 我收到错误 Class methods are only allowed within classes use static to declare
  • 如何在两点之间填充数组列表?

    我有这段代码 解释 用户输入 initcores 数据和 time 数据 forces 是结果 我想用从 0 到 ttime 的值填充 x 数组 用 y 填充 initcores 到 fcores 并绘制散点图 x 与 y 我有一个问题 如
  • Latex:\bibliographystyle{abbrv} 根据外观排序引用[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我使用 Latex 的 bibliographystyle abbrv 命令作为参考 但引用的顺序并不按照它们在文档中出现的顺序 无论如何
  • MySQLi 准备好的语句返回 false

    我正在尝试使用 MySQLi 在我的数据库上运行多个查询 这是我的代码 stmt mysqli gt prepare SELECT password FROM users WHERE username LIMIT 1 stmt gt bin
  • 无法生成 qdoc 文档

    按照本指南操作http doc snapshot qt project org qdoc qdoc guide conf html http doc snapshot qt project org qdoc qdoc guide conf
  • C++比较C字符串的麻烦

    我编写了以下代码 该代码不起作用 但当我更改它时 第二个代码片段将起作用 int main int argc char argv if argv 1 i This is what does not work Do Something 但如果
  • scafford自动生成crud存储库asp.net5

    您好 我正在使用 Visual Studio 2015 我知道我们可以通过添加新的来生成 CRUD 视图控制器和操作scafford物品 但是代码生成不是很有用 似乎所有数据层都依赖于控制器 所以我的问题是 任何使用脚手架的方法都会生成代码
  • 干燥 if 语句

    我有一个 C 程序 在许多不同的 cpp 文件中 我执行如下操作 if thing1 empty thing2 empty if thing1 property lt thing2 property return func1 else if
  • Windows 上 Symfony 5.0.1 控制台中已弃用的类警告

    执行后php bin console make 实体 用户实体 或php bin console make 迁移我在控制台中收到这些警告 2019 12 13T15 49 53 00 00 info User Deprecated The
  • Angular 2 - 组件内的 formControlName

    我想创建一个可以与 FormBuilder API 一起使用的自定义输入组件 我该如何添加formControlName在组件内部 模板