Lijin Durairaj
Lijin Durairaj

Reputation: 3501

Uncaught (in promise): Error: No provider for String

I am new to angular2 and i am stuck with this error. I have a class like this

@Injectable()
export class VehicleDetails {
    constructor(private policynumber: string, private vinnumber: string) {
        this.policyNumber = policynumber,
            this.vinNumber = vinnumber
    }
    policyNumber: string;
    vinNumber: string;
}

and another class like this

export class Policy {
    constructor() {
    }
    emailAddress: string;
    vehicleDetails: VehicleDetails[]
}

i have imported my class in the modules like this

import { Policy } from './models/Policy';
import { VehicleDetails } from './models/VehicleDetails';
@NgModule({
  declarations: [
    LienholderComponent,
    AppComponent,
    PolicyVehicleComponent
  ],
  imports: [
    RouterModule.forRoot(
      APP_ROUTES,
      { enableTracing: true } // <-- debugging purposes only
    ),
    BrowserModule,
    FileUploadModule,
    ReactiveFormsModule,
    HttpModule
  ],
  providers: [Policy, VehicleDetails],
  bootstrap: [AppComponent]
})
export class AppModule { }

when i comment my constructor in the VehicleDetails class everything works fine, but when i use the contructor, i start to get this error. how to resolve this error, i have two string parameter in the constructor because i would like to add the values dynamically .

my error is this

enter image description here

What i want to achieve constructing the VehicleDetails class like this is

this.policy.emailAddress='[email protected]';
    this.policy.vehicleDetails.push(new VehicleDetails('valueA1','valueA2'));
    this.policy.vehicleDetails.push(new VehicleDetails('valueB1','valueB2'));
    this.policy.vehicleDetails.push(new VehicleDetails('valueC1','valueC2'));

so, i am getting the value dynamically and i want to bind the values to the model as explained in the above code

Upvotes: 7

Views: 2546

Answers (2)

FAISAL
FAISAL

Reputation: 34673

You dont need the @Injectable decorator according to your question update. Change your VehicleDetails to this:

export class VehicleDetails {
    constructor(policynumber: string, vinnumber: string) {
        this.policyNumber = policynumber,
            this.vinNumber = vinnumber
    }
    policyNumber: string;
    vinNumber: string;
}

and remove VehicleDetails from your AppModule providers.

Upvotes: 1

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657338

DI can't create an instance of VehicleDetails because it doesn't have any information about what to pass for private policynumber: string, private vinnumber: string

What you can do is to use the @Inject() decorator

constructor(@Inject('policy') private policynumber: string, @Inject('vin') private vinnumber: string) {

and provide the values like

providers: [Policy, VehicleDetails, {provide: 'policy', useValue: 'abc'}, {provide: 'vin', useValue: 'def'}],

Upvotes: 8

Related Questions