小编Rit*_*XPD的帖子

Angular 2 - 组件内部的formControlName

我想创建一个可以与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)

angular2-forms angular

29
推荐指数
3
解决办法
3万
查看次数

标签 统计

angular ×1

angular2-forms ×1