Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 无法访问角度组件中的数据_Json_Mongodb_Angular_Symfony - Fatal编程技术网

Json 无法访问角度组件中的数据

Json 无法访问角度组件中的数据,json,mongodb,angular,symfony,Json,Mongodb,Angular,Symfony,问题 无法从Symfony访问Angular2组件的数据 我试过的 Angular2片段 dashboard.component.ts dashboard.component.html 我们的后端是symfony2,我们只是将json传递到组件中 当我试图在dashboard.component.html文件中打印{{persons}}时,它没有打印任何内容 有人能建议我在从RESTAPI渲染数据时缺少什么吗 任何建议都欢迎 我发现当angular2组件触发数据函数时,浏览器中未触发Http请求

问题

无法从Symfony访问Angular2组件的数据

我试过的

Angular2片段

dashboard.component.ts

dashboard.component.html

我们的后端是symfony2,我们只是将json传递到组件中

当我试图在dashboard.component.html文件中打印{{persons}}时,它没有打印任何内容

有人能建议我在从RESTAPI渲染数据时缺少什么吗

任何建议都欢迎

我发现当angular2组件触发数据函数时,浏览器中未触发Http请求

您需要调用数据方法。否则,将不会触发http请求

constructor(public http: Http) {
   this.data();
}

此外,似乎您的路由器链接在HTML中是错误的。难道不是吗
[routerLink]=['/detail',person.id]?

我已经解决了这个问题

它们是内存中的2个问题,api或伪bakend是主要问题

app.module.ts

相反,尝试以下方法:

 imports:

 [

  InMemoryWebApiModule.forRoot(InMemoryDataService, {passThruUnknownUrl: true}), // fake in memory API simulation
 ]
如果你使用假提供者

 providers: [
 fakeBackendProvider,//comment this,if you use this xhr request would go 404
 ]
服务台


尝试这些步骤,我确信如果http请求得到404,这会起作用。

你在哪里调用数据方法Hi sachin我发现没有发出xhr请求。你能提出一些建议吗?没有错误,我的意思是没有发出AJAX请求,这就是为什么没有数据。是的,你需要在构造函数中调用数据方法,就像我在回答中提到的那样。你能告诉我吗为什么触发数据函数时会在浏览器中触发http请求
constructor(public http: Http) {
   this.data();
}
imports:

 [

   InMemoryWebApiModule.forRoot(InMemoryDataService)
 ]
 imports:

 [

  InMemoryWebApiModule.forRoot(InMemoryDataService, {passThruUnknownUrl: true}), // fake in memory API simulation
 ]
 providers: [
 fakeBackendProvider,//comment this,if you use this xhr request would go 404
 ]
private headers = new Headers({'Content-Type': 'application/json','Access-Control-Allow-Origin':'*'});

private heroesUrl = 'http://localhost/api';  

getApiData(): Promise<ApiDashboard[]> {
            return this.http.get(this.heroesUrl)
           .toPromise()
           .then(response => response.json() as ApiDashboard[])
           .catch(this.handleError);
             }