I'm using a directive to display a dialog and confirm continuation, but when returning from the directive my parent component has lost it's original scope. The console.log will return undefined.
Process: when clicking the submit button appConfirm (directive) will handle the click event and display a dialog to continue (yes/no). If yes is selected it will return back to my parent component and execute the submit function, but the scope is lost.
Any suggestions will be helpful. Thanks! A example in Plunker: https://plnkr.co/edit/CoNqz7yv8yaGMMRRarmu
Template:
<button [disabled]="disableButton" [(appConfirm)]="submit" style="primary" class="button-input btn btn-primary center-block">Submit</button>
Directive:
import {Directive, HostListener, Input} from '@angular/core';
import {SharedService} from '../../core/shared.service';
import {MdDialog} from "@angular/material";
import {Continue, Confirmation, ContinueConfirmation} from "../../dialog/dialog.component";
import {FormComponent} from '../../form/form.component'
@Directive({
selector: '[appConfirm]',
})
export class ConfirmDirective {
constructor(
private sharedService: SharedService,
private dialog: MdDialog
) {}
@Input() appConfirm = () => {};
@Input() confirmMessage = 'Are you sure you want to do this?';
@HostListener('click', ['$event'])
confirmFirst(event: Event) {
let dialogRef = this.dialog.open(Confirmation,{disableClose:true});
dialogRef.afterClosed().subscribe(result=>{
if(result) {
this.appConfirm();
}
});
}
}
Component:
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.css']
})
export class FormComponent {
myButton: string = 'test'
submit(){
console.log(this.myButton);
}
}