Angular 2个ContentChild应用Html属性
Angular 2 ContentChild Apply Html Attributes
我有一个自定义组件,可以将 bootstrap 表单组应用于我的表单域。在我的组件中,我具有以下属性:
import { Component, Input, ContentChild } from '@angular/core';
import { NgControl } from '@angular/common';
@Component({
selector: 'form-field',
template: `
<div class="form-group" [ngClass]="{'has-error':(state && !state.valid && state.touched)}">
<label *ngIf="label" [attr.for]="state.name" class="col-sm-3 control-label">{{label}}</label>
<div class="col-sm-9">
<ng-content></ng-content>
<div *ngIf="state && !state.valid && state.errors && state.touched" class="help-block text-danger">
<span *ngIf="state.errors.required">{{label? label:'This field'}} is required</span>
<span *ngIf="state.errors.min">{{label? label:'Value'}} too small</span>
</div>
</div>
</div>
`
})
export class FormFieldComponent{
@Input()
label: string;
@ContentChild(NgControl) state;
}
在我的模板中,我这样使用我的组件:
<form [ngFormModel]="form" (ngSubmit)="onSubmit()" novalidate>
<form-field label="First Name">
<input ngControl="firstName" type="text">
</form-field>
</form>
我想知道有什么方法可以通过我的组件为控件动态设置占位符文本吗?
我希望将标签设置为输入字段的占位符,即
您可以将标签绑定到 "placeholder"
form-field label="First Name">
<input ngControl="firstName" type="text" [attr.placeholder]="label">
</form-field>
此指令适用于所有具有 ngControl
或 [ngControl]="..."
属性的输入元素。它在应用它的元素上设置 placeholder
属性。
使用
全球可用
bootstrap(App, [
provide(PLATFORM_DIRECTIVES, {useValue: [InputLabel], multi: true})
])
@Directive({
selector: ['[ngControl]']
})
export class InputLabel {
@Input()
@HostBinding('attr.placeholder')
label;
constructor() {
console.log('InputLabel');
}
}
在 FormField
组件中查询此指令并将输入中的标签传递给指令(在 ngOnChanges
或 ngAfterContentChecked
中 - 基本上第一次出现 label
和 state
可用。
@Component({
selector: 'form-field',
providers: [],
template: `
<div>
<ng-content></ng-content>
</div>
`,
directives: []
})
export class FormField {
@Input() label: string;
@ContentChild(InputLabel) state;
ngOnChanges() {
if(this.label && this.state) {
this.state.label = this.label;
}
}
ngAfterContentInit() {
if(this.label && this.state) {
this.state.label = this.label;
}
}
}
这只是为了演示它是如何使用的:
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<h2>Hello {{name}}</h2>
<form>
<form-field label="First Name">
<input ngControl="firstName" type="text">
</form-field>
</form>
</div>
`,
directives: [FormField, FORM_DIRECTIVES]
})
export class App {
constructor(fb:FormBuilder) {
this.name = 'Angular2 (Release Candidate!)'
this.form = fb.group({firstName: [""]});
}
}
我选择这种方法是因为我无法通过其他方式查询(NgControl
,...)并获得对输入元素的引用。
没有必要以这种方式提供指令。它也可以像任何其他自定义指令一样提供,方法是将它添加到 @Component()
装饰器上的 directives: [InputLabel]
使用它的地方。
我有一个自定义组件,可以将 bootstrap 表单组应用于我的表单域。在我的组件中,我具有以下属性:
import { Component, Input, ContentChild } from '@angular/core';
import { NgControl } from '@angular/common';
@Component({
selector: 'form-field',
template: `
<div class="form-group" [ngClass]="{'has-error':(state && !state.valid && state.touched)}">
<label *ngIf="label" [attr.for]="state.name" class="col-sm-3 control-label">{{label}}</label>
<div class="col-sm-9">
<ng-content></ng-content>
<div *ngIf="state && !state.valid && state.errors && state.touched" class="help-block text-danger">
<span *ngIf="state.errors.required">{{label? label:'This field'}} is required</span>
<span *ngIf="state.errors.min">{{label? label:'Value'}} too small</span>
</div>
</div>
</div>
`
})
export class FormFieldComponent{
@Input()
label: string;
@ContentChild(NgControl) state;
}
在我的模板中,我这样使用我的组件:
<form [ngFormModel]="form" (ngSubmit)="onSubmit()" novalidate>
<form-field label="First Name">
<input ngControl="firstName" type="text">
</form-field>
</form>
我想知道有什么方法可以通过我的组件为控件动态设置占位符文本吗?
我希望将标签设置为输入字段的占位符,即
您可以将标签绑定到 "placeholder"
form-field label="First Name">
<input ngControl="firstName" type="text" [attr.placeholder]="label">
</form-field>
此指令适用于所有具有 ngControl
或 [ngControl]="..."
属性的输入元素。它在应用它的元素上设置 placeholder
属性。
使用
全球可用bootstrap(App, [
provide(PLATFORM_DIRECTIVES, {useValue: [InputLabel], multi: true})
])
@Directive({
selector: ['[ngControl]']
})
export class InputLabel {
@Input()
@HostBinding('attr.placeholder')
label;
constructor() {
console.log('InputLabel');
}
}
在 FormField
组件中查询此指令并将输入中的标签传递给指令(在 ngOnChanges
或 ngAfterContentChecked
中 - 基本上第一次出现 label
和 state
可用。
@Component({
selector: 'form-field',
providers: [],
template: `
<div>
<ng-content></ng-content>
</div>
`,
directives: []
})
export class FormField {
@Input() label: string;
@ContentChild(InputLabel) state;
ngOnChanges() {
if(this.label && this.state) {
this.state.label = this.label;
}
}
ngAfterContentInit() {
if(this.label && this.state) {
this.state.label = this.label;
}
}
}
这只是为了演示它是如何使用的:
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<h2>Hello {{name}}</h2>
<form>
<form-field label="First Name">
<input ngControl="firstName" type="text">
</form-field>
</form>
</div>
`,
directives: [FormField, FORM_DIRECTIVES]
})
export class App {
constructor(fb:FormBuilder) {
this.name = 'Angular2 (Release Candidate!)'
this.form = fb.group({firstName: [""]});
}
}
我选择这种方法是因为我无法通过其他方式查询(NgControl
,...)并获得对输入元素的引用。
没有必要以这种方式提供指令。它也可以像任何其他自定义指令一样提供,方法是将它添加到 @Component()
装饰器上的 directives: [InputLabel]
使用它的地方。