简体   繁体   English

如何更改另一个组件属性的值? (角度2)

[英]How to change the value of another component property? (Angular 2)

I have a mane page that have a button that triggers a pop up box to appear: 我有一个鬃毛页面,其中包含一个按钮,该按钮会触发一个弹出框出现:

  <i class="material-icons" (click)="openPopUp()">info</i>

openPopUp() is just setting the property to true: openPopUp()只是将属性设置为true:

openPopUp() {
    this.showPopup = true;
  }

and then im sending the value of showPopUp to the PopupComponent(its separate component): 然后,我将showPopUp的值发送到PopupComponent(其单独的组件):

<div *ngIf="showPopup === true">
  <pop-up-info-box [componentBPopUp]="showPopup"></pop-up-info-box>
</div>

but now in the componenetB i have the closePopUp func that is triggered from the html: 但是现在在componenetB中,我具有从html触发的closePopUp函数:

close

and is just setting the componentBPopUp to be false: 并且只是将componentBPopUp设置为false:

  @Input public componentBPopUp: boolean; public closePopUp() { this.popUp = false; } 

but what actually need to be set to false is the showPopup in the first component...how do I set its value properly? 但是实际上需要设置为false的是第一个组件中的showPopup ...如何正确设置其值?

thanks 谢谢

What you should do in ComponentB is to raise an event on close. 您在ComponentB中应该做的是在关闭时引发一个事件。

@Output() onClose: EventEmitter<boolean> = new EventEmitter();


public closePopUp() {
    this.onClose.emit(true);
}

Now on the parent just subscribe to this event: 现在,父级只需订阅此事件:

<div *ngIf="showPopup === true">
  <pop-up-info-box [componentBPopUp]="showPopup" (onClose)="showPopup = false">   
  </pop-up-info-box>
</div>

You should use @Output in componentBPopUp. 您应该在componentBPopUp中使用@Output。

for example: 例如:

  close(){
    this.showPopup = false;
  }

Example of what you trying to do: 您尝试执行的操作示例:

https://plnkr.co/edit/kUWrlnoXgJ15rXObdaqh?p=preview https://plnkr.co/edit/kUWrlnoXgJ15rXObdaqh?p=preview

Good Luck!!! 祝好运!!!

To your question a parent component can refer to a child component using the @ViewChild decorator 对于您的问题,父组件可以使用@ViewChild装饰器引用子组件。

@ViewChild('popup')
popup: ModalComponent;

this.popup.doStuff();

<div  *ngIf="showPopup === true">
  <pop-up-info-box #popup [componentBPopUp]="showPopup"></pop-up-info-box>
</div>

But your specific problem can be solved with two-way data binding between the componentBPopUp and the showPopup field (see here ) 但是您可以通过componentBPopUp和showPopup字段之间的双向数据绑定来解决您的特定问题(请参阅此处

Another option is to use EventEmmiter to notify the other component of the change (see here ) 另一个选择是使用EventEmmiter通知更改的其他组件(请参见此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何跟踪 Angular 中另一个组件中一个组件中发出的值的变化 - How to track change of a value emitted in one component in another component in Angular ReactJS:当另一个组件中的 state 属性值为某个值时,如何更改一个组件中的 state 属性值 - ReactJS: How to change state property value in a component, when the state property value in another component is a certain value 从另一个组件更改组件的属性,并在角度2的html中呈现它 - Change a property of a component from another component and render it in the html in angular 2 从另一个组件中更改一个组件中的属性值 - Change a property's value in one component from within another component 无法从 angular 中的另一个组件更改一个组件的属性值 - unable to change an attribute value of one component from another component in angular 如何在角度2组件中单击时更改布尔值 - How to change boolean value on click in angular 2 component 角度-在服务中更改对象属性值,然后在另一个控制器中使用它 - Angular - Change an object property value in a service and use it in another controller 将角值发送到另一个组件 - Send Angular Value to another component 更改另一个属性时的角度更新属性 - angular update property on change of another property 如何在 Reactjs 上调用可重用组件时更改属性的值? - How can I change the value of a property on the call of a reusable component on Reactjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM