Q:

Angular4 - Check checkbox on cancel

I want to open a confirmation on unchecking a checkbox, and on clicking of cancel I want to check the checkbox.
HTML:
<div class="form-group col-xs-1">
    <input type="checkbox" name="add" (change)="test()"
        [(ngModel)]="add.checked" />{{add.name}}
</div>

Typescript:
import { Component} from '@angular/core';
@Component({
    selector: 'test',
    templateUrl: './test.component.html',
    styleUrls: ['./test.component.css']
})
export class TestComponent {
    add: any = { name: 'Laptops', checked: true };
    constructor() { }
    test() {
        if (!this.add.checked) {
            this.add.checked = !confirm('Are you sure?');
        }
    }
}

Please help

A:

I found answer myself by using setTimeout:
import { Component} from '@angular/core';
@Component({
    selector: 'test',
    templateUrl: './test.component.html',
    styleUrls: ['./test.component.css']
})
export class TestComponent {
    add: any = { name: 'Laptops', checked: true };
    constructor() { }
    test() {
        if (!this.add.checked) {
            let confirm = !confirm('Are you sure?');
            setTimeout(() => {
                this.add.checked = confirm;
            }, 0);       
        }
    }
}

