EDIT: I've discovered what is causing the problem; I'm now looking for a solution as to how to cater for it.
NOTE: This issue is happening in dev mode (i.e. not prod, and not using aot)
I'm using the "Update" solution from here:
https://stackoverflow.com/a/46324043/3164070
To solve an issue with an input in a child component that was not updating the valid flag on the form. It works in a fairly simple test project, however when moving it to our real project, it throws this error:
core.es5.js:1020 ERROR Error: Uncaught (in promise): Error: No provider for NgForm!
Error: No provider for NgForm!
at ZoneAwareError (http://192.168.2.34:4200/vendor.bundle.js:153804:33)
at injectionError (http://192.168.2.34:4200/vendor.bundle.js:1378:90)
at noProviderError (http://192.168.2.34:4200/vendor.bundle.js:1416:12)
at ReflectiveInjector_._throwOrNull (http://192.168.2.34:4200/vendor.bundle.js:2858:19)
at ReflectiveInjector_._getByKeyDefault (http://192.168.2.34:4200/vendor.bundle.js:2897:25)
at ReflectiveInjector_._getByKey (http://192.168.2.34:4200/vendor.bundle.js:2829:25)
at ReflectiveInjector_.get (http://192.168.2.34:4200/vendor.bundle.js:2698:21)
at resolveNgModuleDep (http://192.168.2.34:4200/vendor.bundle.js:9701:25)
at NgModuleRef_.get (http://192.168.2.34:4200/vendor.bundle.js:10771:16)
at resolveDep (http://192.168.2.34:4200/vendor.bundle.js:11259:45)
at _createProviderInstance (http://192.168.2.34:4200/vendor.bundle.js:11102:20)
at resolveDep (http://192.168.2.34:4200/vendor.bundle.js:11238:56)
at createClass (http://192.168.2.34:4200/vendor.bundle.js:11129:32)
at createDirectiveInstance (http://192.168.2.34:4200/vendor.bundle.js:10960:37)
at createViewNodes (http://192.168.2.34:4200/vendor.bundle.js:12401:53)
at ZoneAwareError (http://192.168.2.34:4200/vendor.bundle.js:153804:33)
at injectionError (http://192.168.2.34:4200/vendor.bundle.js:1378:90)
at noProviderError (http://192.168.2.34:4200/vendor.bundle.js:1416:12)
at ReflectiveInjector_._throwOrNull (http://192.168.2.34:4200/vendor.bundle.js:2858:19)
at ReflectiveInjector_._getByKeyDefault (http://192.168.2.34:4200/vendor.bundle.js:2897:25)
at ReflectiveInjector_._getByKey (http://192.168.2.34:4200/vendor.bundle.js:2829:25)
at ReflectiveInjector_.get (http://192.168.2.34:4200/vendor.bundle.js:2698:21)
at resolveNgModuleDep (http://192.168.2.34:4200/vendor.bundle.js:9701:25)
at NgModuleRef_.get (http://192.168.2.34:4200/vendor.bundle.js:10771:16)
at resolveDep (http://192.168.2.34:4200/vendor.bundle.js:11259:45)
at _createProviderInstance (http://192.168.2.34:4200/vendor.bundle.js:11102:20)
at resolveDep (http://192.168.2.34:4200/vendor.bundle.js:11238:56)
at createClass (http://192.168.2.34:4200/vendor.bundle.js:11129:32)
at createDirectiveInstance (http://192.168.2.34:4200/vendor.bundle.js:10960:37)
at createViewNodes (http://192.168.2.34:4200/vendor.bundle.js:12401:53)
at resolvePromise (http://192.168.2.34:4200/vendor.bundle.js:153599:31) [angular]
at resolvePromise (http://192.168.2.34:4200/vendor.bundle.js:153570:17) [angular]
at http://192.168.2.34:4200/vendor.bundle.js:153647:17 [angular]
at Object.onInvokeTask (http://192.168.2.34:4200/vendor.bundle.js:4090:33) [angular]
at ZoneDelegate.invokeTask (http://192.168.2.34:4200/vendor.bundle.js:153284:36) [angular]
at Zone.runTask (http://192.168.2.34:4200/vendor.bundle.js:153052:47) [<root> => angular]
at drainMicroTaskQueue (http://192.168.2.34:4200/vendor.bundle.js:153480:35) [<root>]
at <anonymous> [<root>]
The problem is that the same component can be used without being on a form, so there is no provider for NgForm.
Is there some way of using the viewProviders member dynamically in the case where there is no form?
Yeah, you can provide it as null
in case if there is no parent NgForm
provider:
export function controlContainerFactory(controlContainer?: ControlContainer) {
return controlContainer;
}
...
viewProviders: [
{
provide: ControlContainer,
useFactory: controlContainerFactory,
deps: [[new Optional(), NgForm]]
^^^^^^^^^^
should do the trick
}
]
来源:https://stackoverflow.com/questions/47046351/no-provider-for-ngform-error-in-dev-mode-using-angular-4-4-6