【问题标题】:Angular: directive on custom formControl results in errorAngular:自定义formControl上的指令导致错误
【发布时间】:2019-02-21 10:32:00
【问题描述】:

当我将自定义 idNumber 指令添加到子 HTML 元素时,一切正常。我想在父类中使用它。然而这不起作用。

// custom directive ////////
@Directive({
selector: "[idNumber]"
})
export class IcoDirective implements OnInit {
formControl: FormControl;
constructor(private control: NgControl) { }

ngOnInit() {
    this.formControl = <FormControl>this.control.control;
    this.addValidators(this.formControl);
}

addValidators(formControl: FormControl) {
    formControl.setValidators(Validators.compose(
            [... some validators...]
        ));
    }


// child HTML ////////
<mat-form-field>
    <input matInput idNumber // if I put the directive here, all works fine.
    [formControl]="idNumberFormControl"
</mat-form-field>

// child TS ////////
ngOnInit() {
    this.idFormControl = new FormControl();
    this.idFormControl.valueChanges.subscribe(value => {
    this.manageUserInput(this.idFormControl );
    });
}

insertIntoForm(parentFormGroup: FormGroup): void {
    parentFormGroup.addControl(this.name, this.idFormControl );
}


// parent HTML ////////
<custom-input-string
    idNumber // if I put the directive here (not in child), I get an error.
    [name]="'idNumber'">
</custom-input-string>

// parent TS ////////
@ViewChild(CustomInputStringComponent) child: CustomInputStringComponent;

ngAfterViewInit() {
    setTimeout(() => {
        this.child.insertIntoForm(this.signupForm);
    }, 0);
}


我得到的错误:


ERROR 错误:未捕获(承诺):错误: StaticInjectorError(AppModule)
[MatInput -> NgControl]:
StaticInjectorError(Platform: core)[MatInput -> NgControl]:

NullInjectorError:没有 NgControl 的提供者!
错误: StaticInjectorError(AppModule)[MatInput -> NgControl]:
StaticInjectorError(Platform: core)[MatInput -> NgControl]:

NullInjectorError: NgControl 没有提供者!

我知道这与依赖注入有关,我试图在整个互联网上寻找解决方案。到目前为止没有成功。
感谢大家的帮助。

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    那是因为您不再在输入中。

    您必须使用ContentChild 来获取您想要的组件,然后使用该组件的控件。

    Stackblitz(根据你之前的问题)

    @ContentChild(CustomComponentComponent) component: CustomComponentComponent;
    
    constructor(
    ) { }
    
    ngAfterViewInit() {
      this.component && this.component.control.setValidators([Validators.required]);
    }
    

    【讨论】:

    • 我在这里有点困惑。您能否修改您的答案以适合我的代码?谢谢你。还有 - 'this.component && ...' 在你的 ngAfterViewInit 方法中做了什么?
    • 这是 if 的简写:如果 &amp;&amp; 的左边是假的,&amp;&amp; 的右边就不会运行(我是一个懒惰的 JS 编码器)。我给你的答案和你的代码基本一样,我只是将我的组件命名为CustomComponentComponent,而不是CustomInputStringComponent。重要的是,不要使用ViewChild,使用ContentChild
    • 感谢您对“this.component && ...”内容的解释。我忘记了Javascript实际上是多么奇怪。我thiiis 接近于接受您的回答,因为多亏了您,我才弄明白并且它有效。但是,我希望您在此处和 StackBlitz 上更新您的答案(正如您在评论中所做的那样),以便其他有相同问题的人更容易理解它。非常感谢。
    • 遇到了同样的错误.....我尝试在组件(例如 mat-button)上使用 NgControl 应用指令并得到同样的错误,而在 HTML 组件上它工作美好的。错误消息并没有完全指出我正确的方向。尚未尝试(尚未)构建适用于(自定义)组件(嵌套 HTML 元素)和常规元素的指令。
    猜你喜欢
    • 2016-11-19
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多