angular - Angular4 - 取消选中复选框
问题描述
我想在取消选中复选框时打开确认,在单击取消时我想选中复选框。
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?');
}
}
}
请帮忙
解决方案
我通过使用 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);
}
}
}
推荐阅读
- laravel - Laravel Echo + Websockets + 私人频道
- css - 如何使用 Bootstrap 4 和 Scss 将容器的宽度覆盖为 1440px 并使列同时响应
- android - 在 RecyclerView android 中使用 MediaPlayer 设置 ProgressBar
- c# - File.WriteAllLines 在 C# 中不起作用(Visual Studio 19)
- tfs - 如何在 TFS 2015 中创建一个文件夹来存储构建定义?
- kubernetes - helm 或 K8S 是否使用 spec.selector.matchLabels 覆盖 spec.template.metadata.labels?
- python - 如何使用 pymongo 将字典附加到 mongodb 中的现有键“processed_data”
- python - 有比 dt.weekofyear 更好的解决方案吗?
- python - 安装IDE后需要单独安装语言吗?
- python - 我如何知道 python 中的两个或多个字典中是否有任何“相等的值”?