将其变量设置为 true 或 false 时如何防止复选框全部选中?
How to prevent checkbox from selecting all when setting its variable to true or false?
我制作了一个 accordion list with ionic [v3] 正如您在 link 视频教程中看到的那样 select 选项菜单不在 .html 文件中。菜单中的列表项来自 .json 文件,您可以在我的 .ts 文件中看到
form.html
.....
<ion-item *ngFor="let item of child.children" detail-none class="child-item" text-wrap no-lines>
<ion-label>{{ item.name }} <p style="color: #0077ff;">{{ selected }}</p> </ion-label>
<ion-checkbox item-end [(ngModel)]="value" (click)="tofix(item)"></ion-checkbox>
</ion-item>
.....
form.ts
export class FormPage implements OnInit{
data: any[];
public SelectedItemToFix: string = '';
@Input('item') item: any;
constructor(public navCtrl: NavController,
public navParams: NavParams,
private http: Http,
private toastCtrl: ToastController) {
let localData = this.http.get('assets/data/menus.json').map(res => res.json().items);
localData.subscribe(data => {
this.data = data;
});
this.title = navParams.get('title');
this.subtitle = navParams.get('subtitle');
}
toggleSection(i) {
this.data[i].open = !this.data[i].open;
}
toggleItem(i, j) {
this.data[i].children[j].open = !this.data[i].children[j].open;
}
ngOnInit() {
}
value = false;
public selected: string = '';
async tofix(item){
let toast = await this.toastCtrl.create({
message: `Added item to be fix : ${item.name}`,
duration: 2000
});
console.log(this.value);
this.SelectedItemToFix += `${item.name}`;
if(this.value == true){
this.selected = "Item Selected"
}
else{
this.selected = "Cancelled"
}
toast.present();
}
您正在对相同的所有复选框进行 ngModel value
。
要修复它,您可以在对象上使用 open
属性:this.data[i].children[j].open
。
您也可以删除 public selected: string = '';
并在项目本身中执行此操作,否则您会遇到与值相同的问题。
.....
<ion-item *ngFor="let item of child.children" detail-none class="child-item" text-wrap no-lines>
<ion-label>
{{ item.name }}
<p style="color: #0077ff;">
{{ item.open ? 'Item Selected' : 'Cancelled'}}
</p>
</ion-label>
<ion-checkbox item-end [(ngModel)]="item.open" (click)="tofix(item)"></ion-checkbox>
</ion-item>
.....
我制作了一个 accordion list with ionic [v3] 正如您在 link 视频教程中看到的那样 select 选项菜单不在 .html 文件中。菜单中的列表项来自 .json 文件,您可以在我的 .ts 文件中看到
form.html
.....
<ion-item *ngFor="let item of child.children" detail-none class="child-item" text-wrap no-lines>
<ion-label>{{ item.name }} <p style="color: #0077ff;">{{ selected }}</p> </ion-label>
<ion-checkbox item-end [(ngModel)]="value" (click)="tofix(item)"></ion-checkbox>
</ion-item>
.....
form.ts
export class FormPage implements OnInit{
data: any[];
public SelectedItemToFix: string = '';
@Input('item') item: any;
constructor(public navCtrl: NavController,
public navParams: NavParams,
private http: Http,
private toastCtrl: ToastController) {
let localData = this.http.get('assets/data/menus.json').map(res => res.json().items);
localData.subscribe(data => {
this.data = data;
});
this.title = navParams.get('title');
this.subtitle = navParams.get('subtitle');
}
toggleSection(i) {
this.data[i].open = !this.data[i].open;
}
toggleItem(i, j) {
this.data[i].children[j].open = !this.data[i].children[j].open;
}
ngOnInit() {
}
value = false;
public selected: string = '';
async tofix(item){
let toast = await this.toastCtrl.create({
message: `Added item to be fix : ${item.name}`,
duration: 2000
});
console.log(this.value);
this.SelectedItemToFix += `${item.name}`;
if(this.value == true){
this.selected = "Item Selected"
}
else{
this.selected = "Cancelled"
}
toast.present();
}
您正在对相同的所有复选框进行 ngModel value
。
要修复它,您可以在对象上使用 open
属性:this.data[i].children[j].open
。
您也可以删除 public selected: string = '';
并在项目本身中执行此操作,否则您会遇到与值相同的问题。
.....
<ion-item *ngFor="let item of child.children" detail-none class="child-item" text-wrap no-lines>
<ion-label>
{{ item.name }}
<p style="color: #0077ff;">
{{ item.open ? 'Item Selected' : 'Cancelled'}}
</p>
</ion-label>
<ion-checkbox item-end [(ngModel)]="item.open" (click)="tofix(item)"></ion-checkbox>
</ion-item>
.....