如何创建与原生<input>
标记一样的自定义组件?我想让我的自定义表单控件能够支持ngControl,ngForm,[(ngModel)]。
据我所知,我需要实现一些接口来使我自己的表单控件像本机一样工作。
此外,似乎ngForm指令仅绑定<input>
标记,这是正确的吗?我该如何处理?
让我解释为什么我需要这个。我想包装几个输入元素,使它们能够作为一个单独的输入一起工作。还有其他方法可以解决这个问题吗? 再一次:我想让这个控件像原生一样。验证,ngForm,ngModel双向绑定等。
ps:我使用的是Typescript。
答案 0 :(得分:80)
我不明白为什么我在互联网上找到的每个例子都必须如此复杂。在解释一个新概念时,我认为最简单的工作示例总是最好的。我把它简化了一下:
使用实现ngModel的组件的外部表单的HTML:
EmailExternal=<input [(ngModel)]="email">
<inputfield [(ngModel)]="email"></inputfield>
自包含组件(没有单独的“访问者”类 - 也许我错过了这一点):
import {Component, Provider, forwardRef, Input} from "@angular/core";
import {ControlValueAccessor, NG_VALUE_ACCESSOR, CORE_DIRECTIVES} from "@angular/common";
const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR = new Provider(
NG_VALUE_ACCESSOR, {
useExisting: forwardRef(() => InputField),
multi: true
});
@Component({
selector : 'inputfield',
template: `<input [(ngModel)]="value">`,
directives: [CORE_DIRECTIVES],
providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]
})
export class InputField implements ControlValueAccessor {
private _value: any = '';
get value(): any { return this._value; };
set value(v: any) {
if (v !== this._value) {
this._value = v;
this.onChange(v);
}
}
writeValue(value: any) {
this._value = value;
this.onChange(value);
}
onChange = (_) => {};
onTouched = () => {};
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
}
事实上,我刚刚将所有这些内容抽象为一个抽象类,我现在使用ngModel扩展每个组件。对我来说,这是一大笔开销和样板代码,我可以不用。
编辑:这是:
import { forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
export abstract class AbstractValueAccessor implements ControlValueAccessor {
_value: any = '';
get value(): any { return this._value; };
set value(v: any) {
if (v !== this._value) {
this._value = v;
this.onChange(v);
}
}
writeValue(value: any) {
this._value = value;
// warning: comment below if only want to emit on user intervention
this.onChange(value);
}
onChange = (_) => {};
onTouched = () => {};
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
}
export function MakeProvider(type : any){
return {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => type),
multi: true
};
}
这是一个使用它的组件:(TS):
import {Component, Input} from "@angular/core";
import {CORE_DIRECTIVES} from "@angular/common";
import {AbstractValueAccessor, MakeProvider} from "../abstractValueAcessor";
@Component({
selector : 'inputfield',
template: require('./genericinput.component.ng2.html'),
directives: [CORE_DIRECTIVES],
providers: [MakeProvider(InputField)]
})
export class InputField extends AbstractValueAccessor {
@Input('displaytext') displaytext: string;
@Input('placeholder') placeholder: string;
}
HTML:
<div class="form-group">
<label class="control-label" >{{displaytext}}</label>
<input [(ngModel)]="value" type="text" placeholder="{{placeholder}}" class="form-control input-md">
</div>
答案 1 :(得分:76)
事实上,有两件事要实施:
ngModel
本身提供ControlValueAccessor
,用于实现此组件与ngModel
/ ngControl
之间的桥梁我们来试试吧。我想实现一个管理公司标签列表的组件。该组件将允许添加和删除标签。我想添加一个验证,以确保标签列表不是空的。我将在我的组件中定义它,如下所述:
(...)
import {TagsComponent} from './app.tags.ngform';
import {TagsValueAccessor} from './app.tags.ngform.accessor';
function notEmpty(control) {
if(control.value == null || control.value.length===0) {
return {
notEmpty: true
}
}
return null;
}
@Component({
selector: 'company-details',
directives: [ FormFieldComponent, TagsComponent, TagsValueAccessor ],
template: `
<form [ngFormModel]="companyForm">
Name: <input [(ngModel)]="company.name"
[ngFormControl]="companyForm.controls.name"/>
Tags: <tags [(ngModel)]="company.tags"
[ngFormControl]="companyForm.controls.tags"></tags>
</form>
`
})
export class DetailsComponent implements OnInit {
constructor(_builder:FormBuilder) {
this.company = new Company('companyid',
'some name', [ 'tag1', 'tag2' ]);
this.companyForm = _builder.group({
name: ['', Validators.required],
tags: ['', notEmpty]
});
}
}
TagsComponent
组件定义了添加和删除tags
列表中元素的逻辑。
@Component({
selector: 'tags',
template: `
<div *ngIf="tags">
<span *ngFor="#tag of tags" style="font-size:14px"
class="label label-default" (click)="removeTag(tag)">
{{label}} <span class="glyphicon glyphicon-remove"
aria- hidden="true"></span>
</span>
<span> | </span>
<span style="display:inline-block;">
<input [(ngModel)]="tagToAdd"
style="width: 50px; font-size: 14px;" class="custom"/>
<em class="glyphicon glyphicon-ok" aria-hidden="true"
(click)="addTag(tagToAdd)"></em>
</span>
</div>
`
})
export class TagsComponent {
@Output()
tagsChange: EventEmitter;
constructor() {
this.tagsChange = new EventEmitter();
}
setValue(value) {
this.tags = value;
}
removeLabel(tag:string) {
var index = this.tags.indexOf(tag, 0);
if (index != undefined) {
this.tags.splice(index, 1);
this.tagsChange.emit(this.tags);
}
}
addLabel(label:string) {
this.tags.push(this.tagToAdd);
this.tagsChange.emit(this.tags);
this.tagToAdd = '';
}
}
正如您所看到的,此组件中没有输入,只有setValue
个(这里的名称并不重要)。我们稍后使用它来提供ngModel
到组件的值。该组件定义了在组件状态(标签列表)更新时通知的事件。
现在让我们实现此组件与ngModel
/ ngControl
之间的链接。这对应于实现ControlValueAccessor
接口的指令。必须针对NG_VALUE_ACCESSOR
令牌为此值访问者定义提供者(不要忘记使用forwardRef
,因为该指令是在之后定义的。)
该指令将在主机的tagsChange
事件上附加一个事件监听器(即指令附加的组件,即TagsComponent
)。事件发生时将调用onChange
方法。此方法对应于Angular2注册的方法。通过这种方式,它将了解相关表单控件的更改和更新。
更新writeValue
中绑定的值时,将调用ngForm
。在注入附加的组件(即TagsComponent)之后,我们将能够调用它来传递此值(请参阅上一个setValue
方法)。
不要忘记在指令的绑定中提供CUSTOM_VALUE_ACCESSOR
。
以下是自定义ControlValueAccessor
的完整代码:
import {TagsComponent} from './app.tags.ngform';
const CUSTOM_VALUE_ACCESSOR = CONST_EXPR(new Provider(
NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => TagsValueAccessor), multi: true}));
@Directive({
selector: 'tags',
host: {'(tagsChange)': 'onChange($event)'},
providers: [CUSTOM_VALUE_ACCESSOR]
})
export class TagsValueAccessor implements ControlValueAccessor {
onChange = (_) => {};
onTouched = () => {};
constructor(private host: TagsComponent) { }
writeValue(value: any): void {
this.host.setValue(value);
}
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
}
这样,当我删除公司的所有tags
时,valid
控件的companyForm.controls.tags
属性会自动变为false
。
有关更多详细信息,请参阅此文章(与#34; NgModel兼容的组件&#34;部分):
答案 2 :(得分:17)
此链接中有RC5版本的示例:http://almerosteyn.com/2016/04/linkup-custom-control-to-ngcontrol-ngmodel
import { Component, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';
const noop = () => {
};
export const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CustomInputComponent),
multi: true
};
@Component({
selector: 'custom-input',
template: `<div class="form-group">
<label>
<ng-content></ng-content>
<input [(ngModel)]="value"
class="form-control"
(blur)="onBlur()" >
</label>
</div>`,
providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]
})
export class CustomInputComponent implements ControlValueAccessor {
//The internal data model
private innerValue: any = '';
//Placeholders for the callbacks which are later providesd
//by the Control Value Accessor
private onTouchedCallback: () => void = noop;
private onChangeCallback: (_: any) => void = noop;
//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;
this.onChangeCallback(v);
}
}
//Set touched on blur
onBlur() {
this.onTouchedCallback();
}
//From ControlValueAccessor interface
writeValue(value: any) {
if (value !== this.innerValue) {
this.innerValue = value;
}
}
//From ControlValueAccessor interface
registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
//From ControlValueAccessor interface
registerOnTouched(fn: any) {
this.onTouchedCallback = fn;
}
}
然后我们可以按如下方式使用此自定义控件:
<form>
<custom-input name="someValue"
[(ngModel)]="dataModel">
Enter data:
</custom-input>
</form>
答案 3 :(得分:5)
蒂埃里的例子很有帮助。以下是TagsValueAccessor运行所需的导入...
import {Directive, Provider} from 'angular2/core';
import {ControlValueAccessor, NG_VALUE_ACCESSOR } from 'angular2/common';
import {CONST_EXPR} from 'angular2/src/facade/lang';
import {forwardRef} from 'angular2/src/core/di';
答案 4 :(得分:0)
您也可以使用@ViewChild指令解决此问题。这使父级可以完全访问注入子级的所有成员变量和函数。
答案 5 :(得分:0)
我编写了一个有助于减少这种情况的模板库:s-ng-utils
。其他一些答案给出了包装 single 表单控件的示例。使用s-ng-utils
可以非常简单地使用WrappedFormControlSuperclass
来完成:
@Component({
template: `
<!-- any fancy wrapping you want in the template -->
<input [formControl]="formControl">
`,
providers: [provideValueAccessor(StringComponent)],
})
class StringComponent extends WrappedFormControlSuperclass<string> {
// This looks unnecessary, but is required for Angular to provide `Injector`
constructor(injector: Injector) {
super(injector);
}
}
在您的帖子中提到要将多个表单控件包装到单个组件中。这是一个使用FormControlSuperclass
进行操作的完整示例。
import { Component, Injector } from "@angular/core";
import { FormControlSuperclass, provideValueAccessor } from "s-ng-utils";
interface Location {
city: string;
country: string;
}
@Component({
selector: "app-location",
template: `
City:
<input
[ngModel]="location.city"
(ngModelChange)="modifyLocation('city', $event)"
/>
Country:
<input
[ngModel]="location.country"
(ngModelChange)="modifyLocation('country', $event)"
/>
`,
providers: [provideValueAccessor(LocationComponent)],
})
export class LocationComponent extends FormControlSuperclass<Location> {
location!: Location;
// This looks unnecessary, but is required for Angular to provide `Injector`
constructor(injector: Injector) {
super(injector);
}
handleIncomingValue(value: Location) {
this.location = value;
}
modifyLocation<K extends keyof Location>(field: K, value: Location[K]) {
this.location = { ...this.location, [field]: value };
this.emitOutgoingValue(this.location);
}
}
然后,您可以将<app-location>
与[(ngModel)]
,[formControl]
,自定义验证器结合使用-Angular支持的控件可以直接使用。
答案 6 :(得分:-1)
为什么要在使用内部ngModel时创建新的值访问器。每当您创建一个包含输入[ngModel]的自定义组件时,我们就已经在实例化一个ControlValueAccessor。这就是我们需要的访问者。
模板:
<div class="form-group" [ngClass]="{'has-error' : hasError}">
<div><label>{{label}}</label></div>
<input type="text" [placeholder]="placeholder" ngModel [ngClass]="{invalid: (invalid | async)}" [id]="identifier" name="{{name}}-input" />
</div>
组件:
export class MyInputComponent {
@ViewChild(NgModel) innerNgModel: NgModel;
constructor(ngModel: NgModel) {
//First set the valueAccessor of the outerNgModel
this.outerNgModel.valueAccessor = this.innerNgModel.valueAccessor;
//Set the innerNgModel to the outerNgModel
//This will copy all properties like validators, change-events etc.
this.innerNgModel = this.outerNgModel;
}
}
用作:
<my-input class="col-sm-6" label="First Name" name="firstname"
[(ngModel)]="user.name" required
minlength="5" maxlength="20"></my-input>
答案 7 :(得分:-1)
使用ControlValueAccessor
NG_VALUE_ACCESSOR
非常容易。
您可以阅读本文以创建一个简单的自定义字段 Create Custom Input Field Component with Angular