[英]Passing boolean value is not getting shared from one component to another - Angular
This is just simple usecase, but don't get what I'm missing.这只是一个简单的用例,但不要明白我遗漏了什么。
I've componentA & componentB.我有 componentA 和 componentB。 Passing booloan value via click event from componentB to componentA using eventEmitter , to show/hide particular div in componentA.使用eventEmitter通过单击事件将布尔值从componentB传递到componentA ,以显示/隐藏 componentA 中的特定 div。
So far迄今为止
public showLayOut = false;
@Output() passThisValue = new EventEmitter<boolean>();
ngOnInit(): void {
this.showLayOut = false;
}
triggerEventToPassBoolVal() {
this.someService(baseLink).subscribe((data) => {
if (data[0].status === true) {
this.showLayOut = true;
if (this.showLayOut) {
this.passThisValue.emit(this.showLayOut);
}
}
})
}
<div *ngIf="showLayOut">
<p>Show this div if emitted value is true, otherwise hide</p>
</div>
Could anyone pls help me to point out what i did wrong?, if possible pls share any existing stackblitz working sample谁能帮我指出我做错了什么?如果可能,请分享任何现有的 stackblitz 工作样本
Note: im using form like this to submit data (ngSubmit)="OK(data)"
Thanks注意:我使用这样的表单来提交数据(ngSubmit)="OK(data)"
谢谢
If componentA is parent component of componentB .如果componentA是 componentB 的父组件。 I have that solution.我有那个解决方案。 for ex:例如:
import { Component, OnInit, ViewContainerRef } from '@angular/core';
// componentB.compoent.ts
getParentComponent() {
return this.viewContainerRef['_data'].componentView.component.viewContainerRef['_view'].component;
}
changeBooleanvalueMethod () {
this.getParentComponent().showLayOut = true; // showLayOut is componentA variable
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.