【发布时间】:2021-02-21 10:30:12
【问题描述】:
当复选框中发生单击事件时,我需要使用 ngif 显示和隐藏表单。
我的列标题和我的列值代码
<th><label class="btn btn-filter">
<input type="checkbox" name="allTrades" [value]="trade" (change)="allTrades($event)"> status
</label> </th>
我有状态栏名称,并且附近有一个复选框
<td><ng-container *ngFor="let trd of trade">
<label class="btn btn-filter">
<input type="checkbox" name="trades" [checked]="trd.selected" (change)="yourfunc($event)" (change)="handleSelected($event)">{{ trd.label}}
</label>
</ng-container></td>
我有状态列的值,并且我以这样的方式编写,当我单击列标题中的复选框时,列中的所有复选框值都会启用,反之亦然
启用复选框时我想显示的内容
我需要的是当复选框被启用时我想显示这个 html 块的内容,当复选框被禁用时它应该被隐藏。
<div *ngif=ischeck>
<mat-form-field class="example-full-width" >
<input class="toolbar-search" type="text" matInput autocomplete="off" >
<mat-placeholder>Name</mat-placeholder>
</mat-form-field>
<mat-form-field class="example-full-width" >
<input class="toolbar-search" type="text" matInput autocomplete="off">
<mat-placeholder>City</mat-placeholder>
</mat-form-field>
</div>
我的 ts。这里的 handleSelected($event) 是我试图隐藏 html 的点击事件函数
trade = [
{ label: ' Check', selected: false },
];
allTrades(event) {
const checked = event.target.checked;
this.trade.forEach(item => item.selected = checked);
}
handleSelected($event) {
if ($event.target.checked === true) {
// Handle your code
this.ischeck=true
}
}
注意:此处编写的复选框代码的方式是,我在列名附近有一个复选框,当我启用它时,列值中的所有复选框都会启用,反之亦然。
https://stackblitz.com/edit/angular-ivy-zahy1b?file=src%2Fapp%2Fapp.component.html
我的 stackblitz 链接。当我自动单击状态复选框时,另一个复选框重复相同。那部分我已经完成了。我需要的是,当复选框被启用时,我想在 div 标签中显示内容
【问题讨论】:
-
我真的试过了,但我不明白你想要达到的目标是什么,但不能......我认为你最好在这里创建一个代码片段:stackblitz.com/fork/angular-ivy 并更新你的问题
-
我已经发布了 stackblitz 链接兄弟。我也在我的问题中进一步解释了。如果可以的话请帮忙。 @TheFabio
-
。希望这个对@TheFabio有所帮助
-
ngif 的正确语法是
*ngIf,如果你修复它就可以了 -
我知道我犯了一个错误。但不,它不起作用。你能更新堆栈闪电战并发布@TheFabio
标签: angular typescript forms checkbox angular8