1

I am using Angular 2.0 beta version. I need to call a web API from a service. But I didn't get any solution.

mobileService.ts

import {Http, Headers, HTTP_PROVIDERS} from 'angular2/http';
import {Injectable} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
import { Mobile } from './datatypes/Mobile';

@Injectable()
export class MobileService {          
     constructor(public http: Http) {     
        this.http = http;
        }

getMobiles() {        
    // return an observable
    return this.http.get("api/ElectronicsAPI/Get")
        .map((responseData) => {
            return responseData.json();
        })
        .map((mobiles: Array<any>) => {
            let result: Array<Mobile> = [];
            if (mobiles) {
                mobiles.forEach((mobile) => {
                    result.push(
                        new Mobile(mobile.name,
                            mobile.price));
                });
            }
            return result;
        });
}
}

Error is,

  • this.http.get(...).map is not a function

Also I haved some questions related to this topics. In that, they mention to import map as,

  • import 'Rxjs/add/operator/map'

But how can I do that in TypeScript?

Is there any other way to import plugin for the map function ?

boot.ts

import {bootstrap}    from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {AppComponent} from './app.component';
import {HTTP_PROVIDERS} from 'angular2/http';

bootstrap(AppComponent,[ROUTER_PROVIDERS,HTTP_PROVIDERS]);

1 Answer 1

1

You can use the following within your mobileService.ts file:

import 'Rxjs/add/operator/map'

This import should be done where you use the map method.

The following answers could help you as well:

Hope it helps you, Thierry

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response... But, i have some doubts in this import statement... How it will be mapped? Shall i include any script file in the index.html
In fact, you should have the Rx.js already included within your index.html file using a script element. The import must be done in the mobileService.ts file...
Now i got the solution. After doing some changes in the system.config

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.