简体   繁体   English

Angular嵌套组件未使用ngIf渲染

[英]Angular Nested Component not rendering with ngIf

I am simply trying to nest a component inside a component and pass an array as the ngFor of the nested component. 我只是试图将一个组件嵌套在一个组件内,然后将数组作为嵌套组件的ngFor传递。 The issue is the array is from an observable and nothing is rendering for the nested component. 问题是数组来自可观察的对象,嵌套组件没有任何渲染。 I can see the observable returning in the browser console so I know everything is working up to the point of the nested component render. 我可以在浏览器控制台中看到可观察到的返回结果,因此我知道一切都在进行,直到嵌套组件渲染为止。 I know it has something to do with the render occurring before the observable returning data but I have not been able to find documentation or online information (or SO questions) that have pointed me to what I am missing. 我知道它与可观察到的返回数据之前发生的渲染有关,但是我无法找到文档或在线信息(或SO问题),这些信息使我指出了我所缺少的内容。

Here is the code.. 这是代码。

srv-data.service.ts: srv-data.service.ts:

 interface IWeek { id: number; weeknumofyear: number; year: number; holidaymontype: number; holidaytuetype: number; holidaywedtype: number; holidaythutype: number; holidayfritype: number; } @Injectable() export class SrvDataService { errorMessage: any; private _apiUrl = 'http://172.16.194.250:99/api/'; weeks$: Observable<IWeek[]>; weeks: IWeek[] = []; constructor(private _http: HttpClient) { this.getWeeks() .subscribe(weeks$ => this.weeks = weeks$, error => this.errorMessage = <any>error); } getWeeks(): Observable<IWeek[]> { return this._http.get<IWeek[]>(this._apiUrl + 'Weeks') .do(data => console.log('Weeks from getWeeks(): ' + JSON.stringify(data))) .catch(this.handleError); } } 

app.component.ts: app.component.ts:

 import { Component, OnInit } from '@angular/core'; import { SrvDataService } from './services/srv-data.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', }) export class AppComponent { weeks: IWeek[] = []; constructor (public dataService: SrvDataService) { this.weeks = this.dataService.weeks; } } 

app.component.html: app.component.html:

 <div> In app.component {{ weeks }} <app-week *ngFor="let week of weeks; index as i; first as isFirst" [week]="week"></app-week> </div> 

Any advice is greatly appreciated! 任何意见是极大的赞赏!

Delete subscribe from the service and use async Pipe inside your app.component.html: 从服务中删除订阅,然后在app.component.html中使用异步管道:

<div>
  In app.component {{ weeks }}
  <app-week *ngFor="let week of weeks | async; index as i; first as isFirst"
            [week]="week">
  </app-week>
</div>

Don't Subscribe the Observable in Service, Try Subscribing it in Components ngOnInit and assign the response in "this.weeks". 不要订阅服务中的Observable,尝试在ngOnInit组件中进行订阅,并在“ this.weeks”中分配响应。 Hope That will help. 希望对您有所帮助。

Debug weeks like "{{weeks | json}}". 调试像“ {{weeks | json}}”这样的星期。 There you should see whole object else you will see "[object Object]". 在那里,您应该看到整个对象,否则将看到“ [object Object]”。

If that Doesnot work, Please share it over stackblitz, i can fix it for you 如果不起作用,请通过stackblitz分享,我可以为您修复

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

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