bier hier bier hier - 1 year ago 256
AngularJS Question

How to fix angular 2 error Cannot read property 'getOptional'?

I am getting my head around angular 2 and trying to define a service called searchservice and inject this in the bootstap part:

import {SearchService} from 'src/service';

This is what the service looks like:

import {Injectable} from 'angular2/core';
import {URLSearchParams, Jsonp} from 'angular2/http';

export class SearchService {
constructor(private http: Http) {}

search (name: string) {

However how can I fix this error that comes up when running the app?:

VM337 angular2-polyfills.js:138 Error: Cannot read property 'getOptional' of undefined
Error loading
at _runAppInitializers (
at PlatformRef_._initApp (
at PlatformRef_.application (
at Object.bootstrap (
at execute (!transpiled:60:23)
at u (
at Object.execute (
at y (
at w (

plunkr ref:

Answer Source

The important part of the error is the line above the one you listed, which says:

Cannot resolve all parameters for SearchService(?). Make sure they all have valid type or annotations.

Your search service is trying to define a variable of type Http but you didn't import Http into the service.

change the import line to import {URLSearchParams, Jsonp, Http} from 'angular2/http';.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download