获取控件组件内部的控件属性
Get control properties inside control component
我创建了一个自定义输入组件,但我想处理组件内部的错误。所以为了使验证工作,我需要从控制对象中获取错误。可能吗?
我的组件和 here 完全一样。
组件的顶部:
@Component({
selector: 'sc-input',
styles,
template: `
<label class="label">
<ng-content></ng-content>
<input class="input" [(ngModel)]='value' [attr.name]='name' [attr.type]='type'
(blur)='onBlur($event)' (focus)='onFocus($event)'>
<div class="errors">
<div class="errors__messages"><ng-content select="sc-error-messages"></ng-content></div>
<div class="errors__indicator"></div>
</div>
</label>
`,
providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]
})
export class CustomInputComponent implements ControlValueAccessor {
...
}
解决了 :host 类 的问题,但仍然想知道是否有办法在其组件内部进行控制..
我认为这在新的表单模块中应该仍然有效:
export class CustomInputComponent implements ControlValueAccessor {
constructor(private control:NgControl, private ngModel:NgModel) {}
}
更新
刚找到https://github.com/angular/angular/issues/7391#issuecomment-246120184
建议
constructor(private _injector:Injector) {
this.control = this._injector.get(NgControl).control;
}
我创建了一个自定义输入组件,但我想处理组件内部的错误。所以为了使验证工作,我需要从控制对象中获取错误。可能吗?
我的组件和 here 完全一样。
组件的顶部:
@Component({
selector: 'sc-input',
styles,
template: `
<label class="label">
<ng-content></ng-content>
<input class="input" [(ngModel)]='value' [attr.name]='name' [attr.type]='type'
(blur)='onBlur($event)' (focus)='onFocus($event)'>
<div class="errors">
<div class="errors__messages"><ng-content select="sc-error-messages"></ng-content></div>
<div class="errors__indicator"></div>
</div>
</label>
`,
providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]
})
export class CustomInputComponent implements ControlValueAccessor {
...
}
解决了 :host 类 的问题,但仍然想知道是否有办法在其组件内部进行控制..
我认为这在新的表单模块中应该仍然有效:
export class CustomInputComponent implements ControlValueAccessor {
constructor(private control:NgControl, private ngModel:NgModel) {}
}
更新
刚找到https://github.com/angular/angular/issues/7391#issuecomment-246120184
建议
constructor(private _injector:Injector) {
this.control = this._injector.get(NgControl).control;
}