[英]Angular9's reactive forms & atomic design
I am implementing atomic design in my Angular 9 application.我在我的 Angular 9 应用程序中实现原子设计。 This means that I will build my pages with atoms, molecules & organisms.
这意味着我将用原子、分子和有机体构建我的页面。 All is going fine, except for the ReactiveFormsModule.
一切都很好,除了 ReactiveFormsModule。
I want to convert the <input />
to its own component, so that I don't have to duplicate the associated HTML all the time.我想将
<input />
转换为它自己的组件,这样我就不必一直复制关联的 HTML。 However, reactive forms is not having any of it.但是,反应式 forms 没有任何它。
The sample below is returns an error onload: ERROR Error: No value accessor for form control with name: 'field2'
下面的示例在加载时返回错误:
ERROR Error: No value accessor for form control with name: 'field2'
I made a StackBlitz example with the full code.我用完整的代码做了一个StackBlitz例子。
app.component.ts app.component.ts
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
@Component({selector: 'app-root',templateUrl: './app.component.html'})
export class AppComponent {
form: FormGroup;
constructor(fb: FormBuilder) {
this.form = fb.group({
field1: ['value1', [Validators.required]],
field2: ['value2', [Validators.required]],
});
}
onSubmit() {console.log(this.form.value);}
}
app.component.html Here I tried replacing the second input with the atom. app.component.html在这里我尝试用原子替换第二个输入。
<form [formGroup]="form" (ngSubmit)="onSubmit();">
<label>
Field 1 <input formControlName="field1" />
</label>
<label>
Field 2 <app-input formControlName="field2"></app-input>
</label>
<button type="submit">Submit</button>
</form>
input.component.ts输入组件.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-input',
template: '<input [formControlName]="formControlName" />',
})
export class InputComponent implements OnInit {
@Input() formControlName: string;
constructor() { }
ngOnInit(): void {
}
}
I have tried to implement a ControlValueAccessor
, through this tutorial but this resulted in weird behaviour.我试图通过本教程实现
ControlValueAccessor
,但这导致了奇怪的行为。
Can anyone show me how to achieve this?谁能告诉我如何实现这一目标?
if u want to make ur life easy then use FormControl as input from ur custom component this is code from my app如果你想让你的生活变得轻松,那么使用 FormControl 作为你的自定义组件的输入,这是我的应用程序中的代码
// custom component
@Input() set control(value: FormControl) {
if (this._control !== value) {
this._control = value;
}
}
// tempalte
<input [formControl]="_control">
input parent control i not using formBuilder but fromControl and formGroup directly.输入父控件我不使用 formBuilder 而是直接使用 fromControl 和 formGroup 。
name = new FormControl('');
constructor(){
let name = this.name;
this.formGroup = new FromGroup({name });
// template
<custom-control [control]="name">
app.component.ts app.component.ts
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
@Component({selector: 'app-root',templateUrl: './app.component.html'})
export class AppComponent {
form: FormGroup;
constructor(fb: FormBuilder) {
this.form = fb.group({
field1: ['value1', [Validators.required]],
field2: ['value2', [Validators.required]],
});
}
onSubmit() {console.log(this.form.value);}
}
app.component.html app.component.html
<form [formGroup]="form" (ngSubmit)="onSubmit();">
<label>
Field 1 <input formControlName="field1" />
</label>
<label>
Field 2 <app-input [control]="form.controls.field2"></app-input>
</label>
<button type="submit">Submit</button>
</form>
input.component.ts输入组件.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-input',
template: '<input [formControl]="formControl" />',
})
export class InputComponent implements OnInit {
@Input() set control(value: FormControl) {
if (this.formControl !== value) {
this.formControl = value;
}
}
formControl: FormControl;
constructor() { }
ngOnInit(): void { }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.