我想创建一个可以与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>
Run Code Online (Sandbox Code Playgroud)
零件:
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;
}
Run Code Online (Sandbox Code Playgroud)
用法:
<custom-input label="Title"
formControlName="title" // Pass this to input inside the component>
</custom-input>
Run Code Online (Sandbox Code Playgroud)