简体   繁体   English

传递 boolean 值不会从一个组件共享到另一个组件 - Angular

[英]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迄今为止

componentB组件B

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);
            }
        }
    })
}

componentA.html组件A.html

<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.

相关问题 将“用户名”值从一个组件传递到Angular 2中的另一个组件 - Passing “username” value from one component to another component in Angular 2 将一个组件的值传递给另一个Angular - Passing value of one component to another Angular Angular 8 从另一个组件读取 boolean 值到根组件 - Angular 8 read boolean value from another component to root component 将价值从一种成分传递到另一种成分 - Passing value from one component to another Angular 2-将布尔值“ true”从一个组件传递到另一组件(非同级组件) - Angular 2 - pass boolean “true” from one component to another (not siblings) 在Angular中将对象从一个组件传递到另一个组件 - Passing object from one component to another component in Angular 将 Angular 组件传递给另一个组件 - Passing Angular component to another one 从 Angular 10 中的另一个组件获取价值 - Getting value from another component in Angular 10 从 Angular 7 中的 API 获得响应后,如何将一个组件的值传递给另一个组件 - How to pass value one component to another component after getting response from an API in Angular 7 阻止一个组件从另一个组件angular6获取CSS - Block one component getting CSS from another component angular6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM