Laxmikant Dange
Laxmikant Dange

Reputation: 7688

No provider for NgModel! while creating Structural directive

I created an structural directive. Here is the constructor of that class.

constructor(private templateRef: TemplateRef<any>, private viewContainer: ViewContainerRef, public query: NgModel) {
    console.log(this.query)
}

If I remove public query:NgModel, it works fine. If I add it It throws error.

Error: Uncaught (in promise): No provider for NgModel! (ControlMeta -> NgModel)

What is the error here?

I did same for normal directive, it is working fine.

Upvotes: 5

Views: 6998

Answers (1)

Andrei Zhytkevich
Andrei Zhytkevich

Reputation: 8099

You have to specify NgModel as provider:

@Directive({ 
  selector: '...',
  providers: [NgModel],
  ...
})

Upvotes: 11

Related Questions