Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

angular - Using ngControl caused error: No provider for ControlContainer

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

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

Added the following in my component:

import { FORM_PROVIDERS,FORM_DIRECTIVES } from '@angular/common';

..

directives: [MD_INPUT_DIRECTIVES,
    FORM_DIRECTIVES,
    ],
    providers: [FORM_PROVIDERS],

and I get this error:

browser_adapter.ts:78 EXCEPTION: Error: Uncaught (in promise): Template parse errors:
No provider for ControlContainer ("

[ERROR ->]<md-input placeholder="Amount" value="0" ngControl="ammount" required></md-input>

What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...