2016-07-08 76 views
1

我的angular2應用程序使用ngfor指令顯示來自json數組的數據。以前,我已經將json數組硬編碼到組件中以用於測試目的,並且該應用程序運行良好。現在我已經將硬編碼的json數組轉換爲使用http請求提供json數組的服務。我可以單擊該供應數據的幾次(之間的2-4倍變化)的鏈接,並能正常工作,然後引發錯誤導致錯誤的原因:使用http服務時,插座未被激活?

這是app.routes.ts

... 
{path:'deals', component:Deals}, 
... 

路由器鏈路

<li id="deals" (click)="loadField('deals')"><a [routerLink]="['/deals']"><p class="propertyNavItem" id="dealsText">Deals</p></a></li> 

的交易組件

dev_file:string = "dev_files/deals.json"; 
    list:Array<string>; 

    constructor(private ghttp: GeneralHttpService){} 

    ngOnInit(){ 
    this.ghttp.getRecommendations(this.dev_file).subscribe(
     data=>{ 
     this.list = data.items; 
     }, 
     error => console.log(error), 
    () => console.log("done") 
    ); 
    } 

的ghttp服務

@Injectable() 
export class GeneralHttpService{ 

    constructor(private http:Http){}; 

    getRecommendations(url:string){ 
    return this.http.get(url).map(res => res.json()); 
    } 
} 

拋出的錯誤是這樣的。它看起來與問題相同https://github.com/angular/angular/issues/9479

EXCEPTION: Error: Uncaught (in promise): Error: Outlet is not activated browser_adapter.js:86 
EXCEPTION: Error: Uncaught (in promise): Error: Outlet is not activated browser_adapter.js:77:13 

STACKTRACE: browser_adapter.js:77:13 

[email protected]://localhost:4200/vendor/zone.js/dist/zone.js:538:32 
makeResolver/<@http://localhost:4200/vendor/zone.js/dist/zone.js:515:14 
Zone</ZoneDelegate</[email protected]://localhost:4200/vendor/zone.js/dist/zone.js:323:20 
NgZoneImpl/this.inner<[email protected]://localhost:4200/vendor/@angular/core/src/zone/ng_zone_impl.js:45:32 
Zone</ZoneDelegate</[email protected]://localhost:4200/vendor/zone.js/dist/zone.js:322:20 
Zone</Zone</[email protected]://localhost:4200/vendor/zone.js/dist/zone.js:216:25 
scheduleResolveOrReject/<@http://localhost:4200/vendor/zone.js/dist/zone.js:571:53 
Zone</ZoneDelegate</[email protected]://localhost:4200/vendor/zone.js/dist/zone.js:356:24 
NgZoneImpl/this.inner<[email protected]://localhost:4200/vendor/@angular/core/src/zone/ng_zone_impl.js:36:32 
Zone</ZoneDelegate</[email protected]://localhost:4200/vendor/zone.js/dist/zone.js:355:24 
Zone</Zone</[email protected]://localhost:4200/vendor/zone.js/dist/zone.js:256:29 
[email protected]://localhost:4200/vendor/zone.js/dist/zone.js:474:26 
ZoneTask/[email protected]://localhost:4200/vendor/zone.js/dist/zone.js:426:22 
browser_adapter.js:77:13 

回答

0

http是一個異步操作。您的列表變量在模板呈現時可能尚未填充。

嘗試使用「貓王經營者」在你ngFor,這樣的事情(?):

*ngFor="let listElement of list?" 
+0

謝謝,我試過了,但它沒有幫助。 – Dan

+0

再加上它似乎如果它工作一次或兩次,那麼它應該工作的所有時間,獲取請求是本地文件而不是服務器。 – Dan

+0

看來這是一個公開的問題https://github.com/angular/angular/issues/9479 – Dan

相關問題