angular - Angular中的复选框和增量减量
问题描述
我有 2 个复选框,选中后,它会为该复选框启用“增加/减少”数量按钮。
问题是每当我增加或减少数量时,它都适用于两个复选框。无论哪个复选框被打勾或没有票。请问有人能告诉我哪里错了吗?
以下是我的 TS 代码
quantityInput() {
this.selectLicences.filter((value, index) => {
if (typeof value.quantity === 'string') {
const regexp = new RegExp('^[a-zA-Z]*$')
const specialReg = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/
if (regexp.test(value.quantity)) {
value.quantity = 1
} else if (specialReg.test(value.quantity)) {
value.quantity = 1
} else {
value.quantity = parseInt(value.quantity)
}
console.log(value.name+value.quantity)
}
if (isNullOrUndefined(value.quantity)) {
value.quantity = 1
}
if (value.quantity.toString() === '') {
value.quantity = 1
}
if (value.quantity > 10) {
value.quantity = 10
}
if (value.quantity < 1) {
value.quantity = 1
}
console.log(value.name+value.quantity)
});
}
quantityChanged(action) {
this.selectLicences.filter((value, index) => {
if (typeof value.quantity === 'string')
value.quantity= parseInt(value.quantity)
switch (action) {
case 'add': {
value.quantity >= 10
? (value.quantity= 10)
: (value.quantity = value.quantity + 1)
console.log(value.name+value.quantity)
break
}
case 'sub': {
value.quantity <= 1
? (value.quantity = 1)
: (value.quantity = value.quantity - 1)
console.log(value.name+value.quantity)
break
}
default: {
return
}
}
});
}
以下是我的html代码
<div *ngFor="let licence of selectLicences; let i = index" class="pt2" fxLayout="row wrap" fxLayoutAlign="start" fxLayoutGap="16px grid">
<span fxFlex="25" class="align-right mt4" >
<mat-checkbox
name="selectLicences"
(change)="changeSelection()"
[(ngModel)]="licence.isChecked"
color="primary"
>
</mat-checkbox>
</span>
<span fxFlex="15" class="align-left">
<strong><h3>{{licence.name}}</h3></strong>
</span>
<div class="pt2" fxLayout="row wrap" fxLayoutAlign="center" fxLayoutGap="16px grid">
<span class="mt3">
<button
color="primary"
(click)="quantityChanged('sub')"
mat-flat-button
[disabled]="licence.quantity == 1"
>
<mat-icon svgIcon="Action_Minimize"> </mat-icon>
</button>
</span>
<span fxFlex="35" class="mt3">
<mat-form-field appearance="outline">
<input
min="1"
max="10"
name="selectLicences"
matInput
[disabled]="!licence.isChecked"
(change)="quantityInput()"
[(ngModel)]="licence.quantity"
type="text"
/>
<mat-error *ngIf="licence.quantity" > 10">
Quantity cannot be greater than 10
</mat-error>
<mat-error *ngIf="licence.quantity < 1">
Quantity cannot be lesser than 1
</mat-error>
</mat-form-field>
</span>
<span class="mt3">
<button
(click)="quantityChanged('add')"
color="primary" [disabled]="!licence.isChecked" mat-flat-button>
<mat-icon svgIcon="Action_Add"> </mat-icon>
</button>
</span>
</div>
</div>
解决方案
推荐阅读
- php - 如果中间件是网络,我如何通过邮递员发出请求?
- windows - 如何在 Windows 上升级 pgAdmin 4?
- python - 如何在 Python 中使用 IMAP 仅删除一条特定消息
- python - Power BI 参数是否暴露给 Python 全局变量
- git - Git 默认主目录包含一个错字,我该如何更改它?
- leaflet - 平移时未完全绘制传单折线
- java - iText 2.1.7 中的数字签名
- magento2 - 如何从magento 2中的子产品(简单)中获取选项
- javascript - Mongoose .Populate() 返回空数组
- javascript - Vue.js 不会在输入更新的控制台数据中显示 - 任何错误