请给我一些关于我的应用程序的解决方法的想法!我正在尝试在我的组件中动态切换模板但有奇怪的行为。谢谢!这是堆栈闪电战
我基本上想要什么:我想从我的警报组件中的服务接收异步数据(节点),并在我的#initTemplate 中显示此数据(节点),然后我想获取此数据(节点)的 id,用这个发送请求id 并获取我想在#stylesTemplate 中显示的另一个数据(样式)。这两个模板都在我的警报组件中。
我的问题是什么? 我意识到我的组件所需的行为,但这并不是我所需要的......我在做什么: 1. 单击“pushData”按钮 2.查看我的警报组件 3.单击更改模板按钮(!! 组件消失!!) 4. 再次点击“pushData” 5. 看看我的组件更改了模板
我需要在不消失的情况下切换组件的模板。
这是我的简化警报组件(另请参阅stackblitz工作示例)
class AlertComponent implements OnInit, OnDestroy {
private subscription: Subscription;
message: any;
node$: Observable<{}>;
styles$: Observable<{}>;
constructor(private dataService: DataService) { }
activeInit: boolean = true;
ngOnInit() {
this.node$ = this.dataService.getAsyncData().pipe(share());
this.styles$ = this.node$.pipe(
mergeMap(node => {
if(!!node) {
// I need node here, because getSecondAsyncData send request with this data
return this.dataService.getSecondAsyncData(node);
}
else return of(null);
}));
}
ngOnDestroy() {
}
openSecond() {
this.activeInit = false;
}
openFirst() {
this.activeInit = true;
}
close() {
this.dataService.sendNodeToSubscribe(null);
}
这是我的两个模板的html:
<ng-container *ngTemplateOutlet="activeInit ? initTemplate : stylesTemplate"></ng-container>
<ng-template #initTemplate>
<div class="card left-settings position-fixed" *ngIf="(node$ | async) as node;">
<div class="row justify-content-end mx-0">
<div class="col-0">
<button type="button" class="btn btn-block btn-ghost-light p-1 px-2" (click)="close()">Close</button>
</div>
</div>
<div class="row custom-width">
<div class="col-lg-12">
<button (click)="openSecond()">switch second template</button>
</div>
</div>
</div>
</ng-template>
<ng-template #stylesTemplate>
<div class="card left-settings position-fixed" *ngIf="(styles$ | async) as styles;">
<div class="row justify-content-end mx-0">
<div class="col-0">
<button type="button" class="btn btn-block btn-ghost-light p-1 px-2" (click)="close()">
Close
</button>
</div>
</div>
<div class="row custom-width">
<label>{{styles.isIcon}}</label>
<label>{{styles.radius}}</label>
<button (click)="openFirst()">switch first template</button>
</div>
</div>
</ng-template>
谢谢 !!