获取控件组件内的控件属性 [英] Get control properties inside control component

查看:27
本文介绍了获取控件组件内的控件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义输入组件,但我想处理组件内部的错误.所以为了使验证工作,我需要从控制对象中获取错误.可能吗?

我完全按照此处完成了我的组件.

组件的顶部:

@Component({选择器:'sc-输入',样式,模板:`<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>

`,提供者:[CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]})导出类 CustomInputComponent 实现 ControlValueAccessor {...}

通过:host 类解决了问题,但仍想知道是否有办法控制其组件内部..

解决方案

我认为这应该仍然适用于新的表单模块:

导出类 CustomInputComponent 实现 ControlValueAccessor {构造函数(私有控制:NgControl,私有 ngModel:NgModel){}}

更新

刚刚发现https://github.com/angular/angular/issues/7391#issuecomment-246120184

建议

constructor(private _injector:Injector) {this.control = this._injector.get(NgControl).control;}

I created a custom input component but I want to work with errors inside the component. So to make validation work I need to get errors from control object. Is it possible?

I did my component exactly like here.

Top part of the component:

@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 {
  ...
}

Solved problem by :host classes, but still want to know if there is a way to get control inside its component..

解决方案

I think this should still work in the new forms module:

export class CustomInputComponent implements ControlValueAccessor {
  constructor(private control:NgControl, private ngModel:NgModel) {}
}

Update

Just found https://github.com/angular/angular/issues/7391#issuecomment-246120184

It suggests

constructor(private _injector:Injector) {
   this.control = this._injector.get(NgControl).control;
}

这篇关于获取控件组件内的控件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆