【发布时间】:2018-08-29 17:49:35
【问题描述】:
好吧,我有一个服务条款模式,它是一个 ngBootstrap 模式,当我按下按钮关闭该按钮时,我希望关闭模式的操作定义复选框是否被选中 这是html:
<ng-template #content let-c="close" let-d="dismiss">
<div class="modal-header">
<h4 class="modal-title">Terms of service.</h4>
<button type="button" class="close" aria-label="Close" (click)="d('Cross
click')">
<span aria-hidden="true">×</span>
</button>
</div>
<button type="button" class="btn btn-success" (click)="c('Close click');
setAccepted(true)" >I accept.</button>
</ng-template>
打开模式和复选框的链接
<div class="custom-control custom-checkbox">
<input *ngIf="accepted" type="checkbox" class="custom-
control-input" id="save-info" required>
<label class="custom-control-label" for="save-info">I have read
</label><a href="javascript:void(0)" (click)="open(content)">
the terms of service</a>.
</div>
在它下面我有 <p>{{accepted}}</p> 只是为了测试
还有打字稿
accepted: boolean = false;
constructor(private modalService: NgbModal) {}
open(content) {
this.modalService.open(content);
}
setAccepted(accepted:boolean){
this.accepted = accepted;
}
我尝试了 [(ngModel)], *ngIf, ngModel 到我的打字稿中接受的布尔值,但似乎没有任何效果。
【问题讨论】:
-
您是否要选中该复选框?
*ngIf="accepted"不可能做到这一点? -
是的,我想检查和取消检查它,但为了简单起见,我只把我必须检查的代码放在这里。我首先尝试了 [(ngModel)]="accepted" 但这也不起作用。
-
复选框是否位于
form容器内? -
是的,但为什么这很重要?
-
在这种情况下,
[(ngModel)]在尝试加载模式时一定会导致错误(在控制台中可见)。但是,如果您还向复选框添加名称属性,它应该可以工作:<input name="chkAccepted" [(ngModel)]="accepted" ...>。这是使用带有复选框的双向数据绑定的适当方式。