admin管理员组文章数量:1026989
I have an angular ponent which in turn has two more ponents in it, I want to make sure that second-ponent loads/renders only after the first-ponent is rendered pletely, how can I achieve it?
binedponent.html
<first-ponent></first-ponent>
<second-ponent></second-ponent>
binedponent.ts
import { Component } from '@angular/core';
@Component({
selector: 'main-ponent',
templateUrl: './binedponent.html',
styleUrls: ['./binedponent.css']
})
export class CombimedComponent {
}
I have an angular ponent which in turn has two more ponents in it, I want to make sure that second-ponent loads/renders only after the first-ponent is rendered pletely, how can I achieve it?
bined.ponent.html
<first-ponent></first-ponent>
<second-ponent></second-ponent>
bined.ponent.ts
import { Component } from '@angular/core';
@Component({
selector: 'main-ponent',
templateUrl: './bined.ponent.html',
styleUrls: ['./bined.ponent.css']
})
export class CombimedComponent {
}
Share
Improve this question
asked Aug 6, 2019 at 4:03
Nikhil BharadwajNikhil Bharadwaj
9175 gold badges26 silver badges51 bronze badges
1 Answer
Reset to default 13An easy way to do it would be...
In your first child ponent add an EventEmitter
that fires an event after it has been pletely loaded like so
first-ponent.ponent.ts
import { Component, Output, EventEmitter, AfterViewChecked } from 'angular/core';
// ...
@Output() finishedLoading: EventEmitter<boolean> = new EventEmitter<boolean>();
ngAfterViewChecked() {
// you could also do this after a service call of some sort
this.finishedLoading.emit(true);
}
ngAfterViewChecked
is the last lifecycle hook run on a ponent, so at this point the ponent has been pletely loaded!
You can learn more about lifecycle hooks here
Then in your parent container you can set a flag called hasLoaded
bined.ponent.ts
// ...
hasLoaded: boolean = false;
then in your parents ponent html you can listen for the finishedLoading
event and then render your second ponent like so..
bined.ponent.ts
<!-- Listens for the finishedLoading event and then sets hasLoaded once its been fired -->
<first-ponent (finishedLoading)="hasLoaded = $event"></first-ponent>
<!-- Doesnt render the second ponent until hasLoaded has been set to true -->
<second-ponent *ngIf="hasLoaded"></second-ponent>
I have an angular ponent which in turn has two more ponents in it, I want to make sure that second-ponent loads/renders only after the first-ponent is rendered pletely, how can I achieve it?
binedponent.html
<first-ponent></first-ponent>
<second-ponent></second-ponent>
binedponent.ts
import { Component } from '@angular/core';
@Component({
selector: 'main-ponent',
templateUrl: './binedponent.html',
styleUrls: ['./binedponent.css']
})
export class CombimedComponent {
}
I have an angular ponent which in turn has two more ponents in it, I want to make sure that second-ponent loads/renders only after the first-ponent is rendered pletely, how can I achieve it?
bined.ponent.html
<first-ponent></first-ponent>
<second-ponent></second-ponent>
bined.ponent.ts
import { Component } from '@angular/core';
@Component({
selector: 'main-ponent',
templateUrl: './bined.ponent.html',
styleUrls: ['./bined.ponent.css']
})
export class CombimedComponent {
}
Share
Improve this question
asked Aug 6, 2019 at 4:03
Nikhil BharadwajNikhil Bharadwaj
9175 gold badges26 silver badges51 bronze badges
1 Answer
Reset to default 13An easy way to do it would be...
In your first child ponent add an EventEmitter
that fires an event after it has been pletely loaded like so
first-ponent.ponent.ts
import { Component, Output, EventEmitter, AfterViewChecked } from 'angular/core';
// ...
@Output() finishedLoading: EventEmitter<boolean> = new EventEmitter<boolean>();
ngAfterViewChecked() {
// you could also do this after a service call of some sort
this.finishedLoading.emit(true);
}
ngAfterViewChecked
is the last lifecycle hook run on a ponent, so at this point the ponent has been pletely loaded!
You can learn more about lifecycle hooks here
Then in your parent container you can set a flag called hasLoaded
bined.ponent.ts
// ...
hasLoaded: boolean = false;
then in your parents ponent html you can listen for the finishedLoading
event and then render your second ponent like so..
bined.ponent.ts
<!-- Listens for the finishedLoading event and then sets hasLoaded once its been fired -->
<first-ponent (finishedLoading)="hasLoaded = $event"></first-ponent>
<!-- Doesnt render the second ponent until hasLoaded has been set to true -->
<second-ponent *ngIf="hasLoaded"></second-ponent>
本文标签:
版权声明:本文标题:javascript - Angular, wait for one component to render completely before rendering the other - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1740787020a1775745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论