Angular2 Error: There is no directive with “exportAs” set to “ngForm”

后端 未结 11 2436
小鲜肉
小鲜肉 2020-12-01 13:33

i\'m on the RC4 and i\'m getting the error There is no directive with \"exportAs\" set to \"ngForm\" because of my template :

11条回答
  •  星月不相逢
    2020-12-01 14:12

    If you are getting this instead:

    Error: Template parse errors:

    There is no directive with "exportAs" set to "ngModel"

    Which was reported as a bug in github, then likely it is not a bug since you might:

    1. have a syntax error (e.g. an extra bracket: [(ngModel)]]=), OR
    2. be mixing Reactive forms directives, such as formControlName, with the ngModel directive. This "has been deprecated in Angular v6 and will be removed in Angular v7", since this mixes both form strategies, making it:
    • seem like the actual ngModel directive is being used, but in fact it's an input/output property named ngModel on the reactive form directive that simply approximates (some of) its behavior. Specifically, it allows getting/setting the value and intercepting value events. However, some of ngModel's other features - like delaying updates withngModelOptions or exporting the directive - simply don't work (...)

    • this pattern mixes template-driven and reactive forms strategies, which we generally don't recommend because it doesn't take advantage of the full benefits of either strategy. (...)

    • To update your code before v7, you'll want to decide whether to stick with reactive form directives (and get/set values using reactive forms patterns) or switch over to template-driven directives.

    When you have an input like this:

    
    

    It will show a warning about mixed form strategies in the browser's console:

    It looks like you're using ngModel on the same form field as formControlName.

    However, if you add the ngModel as a value in a reference variable, example:

    
    

    The error above is then triggered and no warning about strategy mixing is shown.

提交回复
热议问题