Angular4 - 选中取消复选框

时间:2018-05-23 09:16:49

标签: angular

我想打开取消选中复选框的确认信息,点击取消时我想勾选复选框。

HTML:

<div class="form-group col-xs-1">
    <input type="checkbox" name="add" (change)="test()"
        [(ngModel)]="add.checked" />{{add.name}}
</div>

打字稿:

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?');
        }
    }
}

请帮忙

1 个答案:

答案 0 :(得分:0)

我通过使用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);       
        }
    }
}