简体   繁体   English

角度通用视图源未显示来自asp.net Web API核心的数据

[英]Angular universal view source not showing data from asp.net web API core

Angular code to load the data 角度代码加载数据

 constructor(private http: HttpClient , private _title: Title,
    private _meta: Meta) {

    this.facts =   this.http.get('https://non-ssr-angular.firebaseio.com/facts.json');
    this.posts =   this.http.get(`${this.baseURL}/api/post/ByMemberAsync/0/None`);
  }

Code to list which is returned API 列出返回API的代码

 <ul> <li *ngFor="let fact of facts | async"> {{fact.text}} </li> </ul> <ul> <li *ngFor="let post of posts | async"> {{post.title}} </li> </ul> <!-- end snippet -> When I view source through the browser it shows list only from (https://non-ssr-angular.firebaseio.com/facts.json) and wen API response show empty. Output <!-- begin snippet: js hide: false console: true babel: false --> 

Please help anyone to fix it 请帮助任何人修复它

can you try out like this , you might get element in view source 您可以这样尝试吗,您可能会在视图源中获取元素

<ul *ngIf="posts">
  <li *ngFor="let post of posts | async">
    {{post?.title}}
  </li>
</ul>

Be sure 确定

in your app.module.ts 在您的app.module.ts中

import { HttpClientModule} from '@angular/common/http'
@NgModule({
  declarations: [
    ...
  ],
  imports: [
    ....
    HttpClientModule,
  ],
  providers: [...],
  bootstrap: [AppComponent]

}) })

in your component.ts 在你的component.ts中

import {HttpClient} from '@angular/common/http';
...
export class myComponent {
   facts:any;
   posts:any;
constructor(..){}

It's work for me: Angular CLI version (1.6.6) 它对我有用:Angular CLI版本(1.6.6)

 import {HttpClient} from '@angular/common/http';
 ...
 facts:any=[];
 this.http.get('https://non-ssr-angular.firebaseio.com/facts.json').subscribe(data=>
 {this.facts=data});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM