[英]Is it possible to put an ngFor inside an ngIf?
I'am trying since two days now to parse a Json file with nested object inside who's himself inside an ngIf...the code will be more explicite I think: 自两天以来,我一直在尝试解析一个Json文件,该文件中的嵌套对象本身就是ngIf ...里面的嵌套对象,我认为代码会更加明确:
So imagine this is a child component opening conditionally from a parent event coming from a button: 因此,想象一下这是一个子组件,它有条件地从来自按钮的父事件中打开:
My http service is working because I already use it in some other components and the observable is "user3" 我的http服务正在工作,因为我已经在其他一些组件中使用了它,并且可观察到的是“ user3”
The parent html: 父html:
<div class="parent">
<img src="fold_down_black.png" type="button"(click)="toggleChild()"/>
<div><my-child [showMePartially]="showVar" ></my-child></div>
</div>
I bind it using input as following in the child.ts: 我在child.ts中使用输入将其绑定如下:
@Input() showMePartially: boolean;
Here's the child.html: 这是child.html:
<div *ngIf="showMePartially" class="child" >
<span id="tfonc" *ngFor="let user3 of userService3.users3 | async">Fonctionnal duration : {{user3.data.operating_rate}}%
</span>
</div>
Here's my service: 这是我的服务:
export interface User3 {
data: any;
}
const usersURL = 'http://my.supa.json;php';
@Injectable()
export class UserService3 {
users3: Observable<User3[]>;
constructor (public http: Http) {
getData() {
const tick3$ = Observable.timer(100, 60000);
return tick3$.flatMap(() => this.http.get(usersURL)).map(res =>
res.json()); // .publishBehavior(<User3[]>[]).refCount();
When i did so nothing in the view and nothing in the console also...thanks in avance for the help..I suppose the conflict is comming from an ngFor inside an ngIf... 当我在视图中没有执行任何操作时,在控制台中也没有执行任何操作……感谢您的帮助。。我想冲突是由ngIf内部的ngFor引起的。
I believe the issue is you're trying to loop through users3
observable. 我相信问题是您正在尝试
users3
可观察的users3
。 You need to subscribe to it instead in your child.ts. 您需要在child.ts中订阅它。 Inside your Child class:
在您的儿童班内:
myUsers = [];
sub: Subscription;
// inject a service. don't forget to import userService3 also
constructor(private userService: userService3) {}
ngOnInit() {
this.sub = this.userService.users3.subscribe(users => this.myUsers = users) // subscribe!
}
ngOnDestroy(){
this.sub.unsubscribe();
}
In your template: 在您的模板中:
*ngFor="let user3 of users3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.