Error: No provider for FormControl
See original GitHub issueI’m submitting a … (check one with “x”)
[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
When my directive constructor injects NgForm
, it throws Error: No provider for NgControl!
Expected behavior It’s working when I using Angular 4.0. So my directive can refer to the form control and listen on value changed.
Minimal reproduction of the problem with instructions
here’s my directive:
import {Directive, HostListener, Input, ElementRef, OnInit} from '@angular/core';
import {NgControl} from '@angular/forms';
export class ValidationMessageDirective implements OnInit {
@Input('validationMessage') customMessage: string;
constructor(private el: ElementRef, private formControl: NgControl, private validationMessageService: ValidationMessageService) {
this.target = $(el.nativeElement);
this.formGroup = this.target.closest('.form-group');
this.formControl.valueChanges.subscribe((newValue) => {
this.checkValidation();
});
}
}
and my module:
import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule, FormControlDirective } from '@angular/forms';
import { ValidationMessageDirective } from './src/ng2-validation-message.directive';
import { ValidationMessageService } from './src/ng2-validation-message.service';
export * from './src/ng2-validation-message.directive';
export * from './src/ng2-validation-message.service';
@NgModule({
imports: [
CommonModule,
FormsModule,
ReactiveFormsModule
],
declarations: [
ValidationMessageDirective
],
exports: [
ValidationMessageDirective
],
})
export class Ng2ValidationMessage {
static forRoot(): ModuleWithProviders {
return {
ngModule: Ng2ValidationMessage,
providers: [ValidationMessageService]
};
}
}
What is the motivation / use case for changing the behavior?
Please tell us about your environment: macOS
- Angular version: 2.0.X
Angular 4.1.x
- Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
all
-
Language: [all | TypeScript X.X | ES6/7 | ES5] TypeScript 2.3.2 ES6
-
Node (for AoT issues):
node --version
=
node 6.10
Issue Analytics
- State:
- Created 6 years ago
- Comments:17 (5 by maintainers)
Top Results From Across the Web
no provider for ngControl [FormControl] Angular 6
As FormControl is exposed as a part of ReactiveFormsModule and NOT the FormsModule ... For anyone using Storybook.js and seeing this error.
Read more >I'm getting the error “Error: No Provider for FormControl” even ...
I'm getting the error “Error: No Provider for FormControl” even FormModule and ReactiveFormModule added to the app.module.ts file.
Read more >No provider for ControlContainer - Material Design for Bootstrap
hi,in MDB Admin Template Pro (Angular version) 6.0.2 I try make a form<form [formGroup]=form (ngSubmit)=onLogin()> <div class=md-form> &
Read more >uncaught (in promise): error: nodeinjector: not_found [ngcontrol]
As FormControl is exposed as a part of ReactiveFormsModule and NOT the FormsModule. Template parse errors: No provider for ControlContainer (“[ERROR ...
Read more >Error: No provider for FormControl-angular.js - appsloveworld
[Solved]-Angular 2 - Error: No provider for FormControl-angular.js ... This problem happens when the input is not a form control, due to I...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@heidermatos and others: If you use angular DI to load
NgControl
:constructor(private ngControl: NgControl) { }
then you cannot declareNG_VALUE_ACCESSOR
as a@Component provider
at the same time.Following does not work and leads to
Cannot instantiate cyclic dependency! NgControl
error:What does work: All you need to do is remove the provider and assign the value accessor manually:
Sorry, I have solved it. This problem happens when the input is not a form control, which is not related to any Angular version.