Angular2,現在處於測試階段,我的公司決定稍微處理它。 我試圖從我的服務設置請求。我瀏覽所有的互聯網,但沒有任何工作。 (也許帖子是在Beta版之前寫的)。@Injectable Service中的HTTP提供者
所以,我有我的boot.ts這樣的:
import {bootstrap} from 'angular2/platform/browser';
import {Component, provide} from 'angular2/core';
import {HTTP_PROVIDERS} from 'angular2/http';
import {BrandsComponent} from './brands/brands.component';
import {BrandsService} from './brands/brands.service';
@Component({
\t selector: 'my-app',
\t template: `
\t \t <brands></brands>
\t `,
\t directives: [BrandsComponent]
})
export class AppComponent {
}
bootstrap(AppComponent, [HTTP_PROVIDERS, BrandsService]);
我BrandsComponent注入我BrandsService。 這裏我的服務代碼:
import {Http} from 'angular2/http';
import {Injectable, Inject} from 'angular2/core';
@Injectable()
export class BrandsService{
\t constructor(public http: Http) {
\t \t console.log('Task Service created.', http);
\t \t http.get('http://google.fr');
\t }
\t getBrands(){
\t \t //return this.http.get('./brands.json');
\t \t return [];
\t }
}
在我的控制檯,我有日誌「創建任務服務」,但任何Ajax請求是怎麼回事。
我不能告訴你我試過了什麼,因爲我改變了我的代碼約10億次。
感謝您的幫助!
@Edit:
這裏我BrandsComponent代碼:
import {Component} from 'angular2/core';
import {Brand} from './brand.interface';
import {BrandsService} from './brands.service';
import {ModelsComponent} from './../models/models.component';
@Component({
\t selector: 'brands',
\t templateUrl: 'templates/brands/list.html',
\t providers: [BrandsService],
\t directives: [ModelsComponent]
})
export class BrandsComponent implements OnInit{
\t public brands;
\t public selectedBrand : Brand;
\t constructor(private _brandsService: BrandsService) { }
\t /*
\t * Get all brands from brands service
\t */
\t getBrands(){
\t \t this.brands = this._brandsService.getBrands();
\t }
\t /*
\t * On component init, get all brands from service
\t */
\t ngOnInit(){
\t \t this.getBrands();
\t }
\t /*
\t * Called when li of brand list was clicked
\t */
\t onSelect(brand : Brand){
\t \t this.selectedBrand = brand;
\t }
}
你能張貼BrandsComponent代碼? – Romain
錯誤是什麼。因爲如果你得到了「任務服務創建」日誌,那麼首先你必須將http.get的值保存在某個地方以供使用,如果我沒有錯的話。 – binariedMe
羅曼:我有我的BrandsComponent,謝謝。 @binariedMe:當然,但任何請求在開發者控制檯中都可見。我將結果賦值給var,並將控制檯記錄下來,但我有Observer對象響應。我搜尋原因,並說明。 – PallMallShow