在使用 NgControl 时,需要将其注入到组件的构造函数中,并且要导入 FormsModule 或 ReactiveFormsModule 模块,以确保这些控件可用。例如:
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-example',
template: `
{{ exampleValue }}
`
})
export class ExampleComponent {
exampleValue: string;
constructor(private ngControl: NgControl) {
console.log(ngControl);
}
}
在这个示例中,我们将 NgControl 注入到 ExampleComponent 的构造函数中,并在模板中使用了 ngModel,因此我们还需要导入 FormsModule 模块。