Using ngControl caused error: No provider for ControlContainer

前端 未结 2 2062
深忆病人
深忆病人 2020-12-19 13:21

I\'m trying to use ngControl for the first time in my app:



        
相关标签:
2条回答
  • 2020-12-19 13:27

    I think that you forgot to wrap the md-input component in form tag, otherwise ngControl won't work:

    <form>
       ...
       <md-input placeholder="Amount" value="0" ngControl="ammount" required></md-input>
       ...
    </form>
    

    This directive can only be used as a child of NgForm or NgFormModel.

    0 讨论(0)
  • 2020-12-19 13:48

    To fix that, you have 2 options: A. in main.ts:

    import {FORM_PROVIDERS} from 'angular2/common';
    
    // other code here
    
    bootstrap(AppCmp, [
      ROUTER_PROVIDERS,
      provide(LocationStrategy, { useClass: HashLocationStrategy }),
      FORM_PROVIDERS
    ]);
    

    B. in your component that uses the form:

    import {FORM_PROVIDERS, FORM_DIRECTIVES} from 'angular2/common';
    @Component({
      providers: [FORM_PROVIDERS],
      directives: [FORM_DIRECTIVES]
    })
    

    Taken from these same issues on github

    • https://github.com/mgechev/angular2-seed/issues/399

    • https://github.com/angular/angular/issues/6374

    May help you.

    0 讨论(0)
提交回复
热议问题