当前位置: 动力学知识库 > 问答 > 编程问答 >

typescript - angular 2 rc4 http requests throwing uncaught (in promise) exception

问题描述:

did something change with angualr 2 http service after the upgrade to angular2 rc4?

I have been going through the docs on angular http client and scoured stack overflow all day, but I could not figure out the problem.

below are snippets of my code implementation and the error I get, can anyone please thell me what I am doing wrong.

this is my code

 import { Restaurant } from '../index'

import { Http, Headers, RequestOptions, Response } from '@angular/http';

import { Injectable } from '@angular/core';

import 'rxjs/Rx';

import { Observable } from 'rxjs/Observable';

@Injectable()

export class RestaurantService {

constructor(private _http: Http){}

addRestaurant(restaurant: Restaurant): Observable<Restaurant> {

let body = JSON.stringify({restaurant});

let headers = new Headers({ 'Content-Type':'application/json'});

let options = new RequestOptions({ headers: headers});

console.log(body);

return this._http.post('http://localhost:3000/restaurant', body, options)

.map(this.extractData)

.catch(this.errorHandler)

}

private extractData(res: Response) {

let body = res.json();

return body;

}

private errorHandler(error:any) {

return Observable.throw(error.json());

}

}

and the service is implemented in my code below

import { Component, OnInit, OnDestroy } from '@angular/core';

import { REACTIVE_FORM_DIRECTIVES, FormArray, FORM_DIRECTIVES, FormControl, FormGroup } from '@angular/forms';

import { Restaurant } from './index';

import { RestaurantService } from '../../index';

@Component({

moduleId: module.id,

selector: 'sd-restaurant-form',

templateUrl: 'restaurant-form.component.html',

styleUrls: ['restaurant-form.component.html'],

directives: [REACTIVE_FORM_DIRECTIVES]

})

export class RestaurantFormComponent implements OnInit, OnDestroy {

formActive: boolean = true;

restaurantForm = new FormGroup( {

.........

.........

});

constructor(public restaurantService: RestaurantService) { }

onSubmit() {

let value = this.restaurantForm.value;

............

............

............

this.restaurantService.addRestaurant(this.restaurant)

.subscribe(

res =>{ this.result = res,

console.log(this.result)},

error=> {this.error = error.

console.log(this.error)}

)

}

ngOnInit() {

for (let i=0; i<24; i++) {

if( i === 0) {

this.timesHour.push(0 +''+i);

}

else if ((Math.log10(i)+1) < 2 ) {

this.timesHour.push(0 +''+i);

} else {

this.timesHour.push(String(i));

}

}

for (let i=0; i<60; i++) {

if( i === 0) {

this.timesMin.push(0 +''+i);

}

else if ((Math.log10(i)+1) < 2 ) {

this.timesMin.push(0 +''+i);

} else {

this.timesMin.push(String(i));

}

}

}

ngOnDestroy() {}

}

and this is the error I get

EXCEPTION: Error: Uncaught (in promise): Can't resolve all parameters for RestaurantFormComponent: (?).

EXCEPTION: Error: Uncaught (in promise): Can't resolve all parameters for RestaurantFormComponent: (?).

STACKTRACE:

Error: Uncaught (in promise): Can't resolve all parameters for RestaurantFormComponent: (?).

at resolvePromise (zone.js?1467669664341:538)

at zone.js?1467669664341:515

at ZoneDelegate.invoke (zone.js?1467669664341:323)

at Object.onInvoke (core.umd.js:9100)

at ZoneDelegate.invoke (zone.js?1467669664341:322)

at Zone.run (zone.js?1467669664341:216)

at zone.js?1467669664341:571

at ZoneDelegate.invokeTask (zone.js?1467669664341:356)

at Object.onInvokeTask (core.umd.js:9091)

at ZoneDelegate.invokeTask (zone.js?1467669664341:355)

Unhandled Promise rejection: Can't resolve all parameters for RestaurantFormComponent: (?). ; Zone: angular ; Task: Promise.then ; Value: BaseException$1 {message: "Can't resolve all parameters for RestaurantFormComponent: (?).", stack: "Error: Can't resolve all parameters for Restaurant…ngular/compiler/bundles/compiler.umd.js:14640:81)"}

Error: Uncaught (in promise): Can't resolve all parameters for RestaurantFormComponent: (?).(…)

网友答案:

Even I had the same problem with angular 2 rc4, importing and using Inject solved the issue of can't resolve all parameters. Here's my code sample:

import { Injectable,Inject } from '@angular/core';
...


@Injectable() 
export class RestaurantService {
   constructor(@Inject(Http) private _http: Http) {
...
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: