2016-07-08 73 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

相关问题