ControlValueAccessor with Error Validation in Angular Material

前端 未结 3 801
耶瑟儿~
耶瑟儿~ 2021-01-18 03:37

I am trying to apply Error Validation style with ControlValueAccessor in custom Material Input Textbox. Ever since applying this custom component, all the red border validat

3条回答
  •  孤独总比滥情好
    2021-01-18 04:37

    Kinda used your answer and the link you provided to come up with this solution:

    
    @Component({
      selector: 'app-custom-input',
      templateUrl: './custom-input.component.html',
      styleUrls: ['./custom-input.component.css'],
      providers: [{
        provide: NG_VALUE_ACCESSOR,
        useExisting: forwardRef(() => CustomInputComponent),
        multi: true
      }]
    })
    export class CustomInputComponent implements OnInit, ControlValueAccessor {
      ...
      _control: NgControl;
      disabled: boolean;
    
      constructor(@Inject(INJECTOR) private injector: Injector) {
      }
    
      ngOnInit() {
        this._control = this.injector.get(NgControl);
      }
      
      ...
    

    custom-input.component.html

    {{Label}}

    Note: I'm not binding to MatInput's outputs (yea). the formControl directive that passes the control to MatInput handles that for us.

    made an example for you

提交回复
热议问题