Attempting to extend FormControlDirective to implement my own FormControl directive results in faulty binding

前端 未结 2 1006
太阳男子
太阳男子 2021-01-03 11:04

I\'m trying to inverse the way forms controls are registering themselves onto a FormGroup, so that instead of having to

@Component({..., templ         


        
2条回答
  •  日久生厌
    2021-01-03 11:41

    You are almost there. There is one more trick though. There isn't DefaultValueAccessor for that input element, thus constructor arguments are populate with null value.

    The formControl \ formControlName selectors appear in one more place - the value accessor. In order your directive to work you should implement all default value accessors for the hybridFormControl directive ( following the pattern for the built-in directives).

    P.S I believe the provider of your directive should be corrected to

    providers: [{
        provide: NgControl, //<-- NgControl is the key
        useExisting: forwardRef(() => HybridFormControlDirective)
    }]
    

提交回复
热议问题