Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 testcase shows No provider for NgControl

ng test shows the below error but it works as expected in reality.

Error: Template parse errors:
    No provider for NgControl ("
      <div class="form-group">
        <label class="control-label">Location</label>
        [ERROR ->]<select class="selectpicker" *ngIf="locations" data-style="btn btn-primary btn-round" title="Select A"): VehicleFormComponent@27:4
    No provider for NgControl ("  <div class="form-group label-floating">
        <label class="control-label">Is Available</label>
        [ERROR ->]<md-slide-toggle [(ngModel)]="isAvailable" color="primary" [ngModelOptions]="{standalone: true}">
       "): VehicleFormComponent@41:4 in src/test.ts (line 25739)
    Expected undefined to be truthy.

I think it's mainly because of adding [ngModelOptions]="{standalone: true}" in a custom tag.

like image 266
Avinash Raj Avatar asked Oct 15 '25 01:10

Avinash Raj


2 Answers

Did you add reference to "FormsModule" in your TestBed module?

import { FormsModule } from '@angular/forms';

TestBed.configureTestingModule({
  imports: [ FormsModule ]
})
.compileComponents();
like image 87
VadimB Avatar answered Oct 17 '25 14:10

VadimB


If anybody else comes here, maybe this will help. I was missing the optional decorator in the constructor:

  constructor(@Optional() @Self() public ngControl: NgControl) {}
like image 34
Akora Avatar answered Oct 17 '25 14:10

Akora