[英]Angular 2 Parent to child boolean in function
I'm trying to emit a boolean change from a parent component to a child component but having no luck. 我正在尝试从父组件向子组件发出布尔更改,但是没有运气。
Parent component: 父组件:
isActionComplete: boolean;
deleteUser() {
this.userService.delete();
this.isActionComplete = true;
setTimeout(() => {
this.isPopUpActive = false;
}, 3000);
this.isActionComplete = false;
}
Parent html: 父html:
<app-pop-up [isActionComplete]="isActionComplete"></app-pop-up>
Child component: 子组件:
@Input() isActionComplete: boolean;
Child html: 子html:
<div *ngIf="isActionComplete">Complete</div>
If I change the 'isActionComplete: boolean;' 如果我更改“ isActionComplete:布尔值;” to 'isActionComplete = true;'
为'isActionComplete = true;' in the parent component it does change the state of the child boolean but for some reason when its in the 'deleteUser' function it does not work.
在父组件中,它确实会更改子布尔值的状态,但是由于某种原因,当它在'deleteUser'函数中不起作用时,它会更改。
Please could someone let me know where im going wrong? 请有人让我知道我要去哪里错了吗?
Thanks 谢谢
Synatx for property binding is [prop]="value"
用于属性绑定的Synatx为
[prop]="value"
<app-pop-up [isActionComplete]="isActionComplete"></app-pop-up>
You need to move the last statement inside setTimeout();
您需要在
setTimeout();
移动最后一条语句setTimeout();
setTimeout()
does not block the execution instead methods or statement inside setTimeout is moved to event-queue and is executed on timeout and statements after the block is executed in the meantime. setTimeout()
不会阻塞执行,而是将setTimeout中的方法或语句移至事件队列,并在超时和语句执行后同时执行该语句。
example: 例:
method a();
setTimeout(()=>{method b()},0})
methhod c();
The order of execution would be 执行顺序为
a();
c();
b();
deleteUser() {
this.userService.delete();
this.isActionComplete = true;
setTimeout(() => {
this.isPopUpActive = false;
this.isActionComplete = false;
}, 3000);
}
I realised what my issue was! 我意识到我的问题是什么! It was the setTimeout causing the problem.
是导致问题的setTimeout。 I needed to move it inside this:
我需要在其中移动它:
deleteUser() {
this.userService.delete();
this.isActionComplete = true;
setTimeout(() => {
this.isPopUpActive = false;
this.isActionComplete = false;
}, 3000);
}
Hope this helps someone out in the future! 希望这对以后的人有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.