简体   繁体   中英

Angular2 - pass variable with EventEmitter by timeout function to parent component

I have issue with Event Emitter in child component.

I want to pass variable with EventEmitter to parent component, but with usage timeout function. Now it's look like this:

CHILD:

export class AlertSuccess implements OnChanges {

    @Input() success: SuccessCRM;
    @Output() emitCollapse:EventEmitter<any> = new EventEmitter();

    private info_ico: string = require('../../public/images/iconInfoTip.png');
    public state: boolean = false;

    constructor() {}

    collapseAlert () {
        let alert = document.getElementById('success');
        setTimeout(function() {
            console.log("BEFORE EMITTED STATE!!!!!: ", this.state );
            console.log(this.emitCollapse);
            this.emitCollapse.emit(this.state); 
        }, 500);
    }

    ngOnChanges(changes: SimpleChanges):void {
        console.log("CHANGES: ", this.success);
        this.collapseAlert();
    }
}

In parent everything is normal, I have method which I passed via template.

Emitter is working without timeout function. Please help me implement it into timeout!

Regards Bosper!

Use Arrow function to point correct this .

setTimeout(() => {
    console.log("BEFORE EMITTED STATE!!!!!: ", this.state );
    console.log(this.emitCollapse);
    this.emitCollapse.emit(this.state); 
}, 500);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM