Reputation: 8889
I could add a dropdown using angular
@Component({
selector: 'app-child-component',
template: `
<select id="select" formControlName = "day" >
<option *ngFor = "let g of dayList" [value] = "g"> {{g}}
</option>
</select>
`,
})
https://plnkr.co/edit/M88wFl?p=preview
But I am unable to add multiple check-boxes
Code below:
@Component({
selector: 'app-child-component',
template: `
Select days in a week :
<td class="even" *ngFor="let item of dayList;let i = index">
<input [(ngModel)]="item.check" type="checkbox" checked="item.check" formControlName = "selectedDays" (change)="updateChkbxArray(n.id, $event.checked, 'selectedDays')" value="n.id" > {{item}}
`,
})
https://plnkr.co/edit/gyAj6W?p=preview
Could you help me in showing the checkbox collection ,
Upvotes: 1
Views: 9706
Reputation: 163
Try the below code for showing multi checkbox and how to get multi selection in component
//HTML
<tr *ngFor="let address of addressList">
<td>
<input type="checkbox" value="{{address.id}}" name="{{address.email}}" (change) ="updateSelectedTimeslots($event)" />
</td>
</tr>
//Conponent
countries: any[] = [];
updateSelectedTimeslots(event) {
let this.countries: any[] = [];
if (event.target.checked) {
if (this.countries.indexOf((event.target.name)) < 0) {
this.countries.push(({'email':event.target.name}));
}
} else {
for(let i = 0; i < this.countries.length; i++) {
let country = this.countries[i];
if(country.email.toLowerCase().indexOf(event.target.name.toLowerCase()) == 0) {
this.countries.splice(this.countries.indexOf(({'email':event.target.name})), 1);
}
}
}
}
Upvotes: 0
Reputation: 59
Since you have used the input element, can you include "FormsModule" from '@angular/forms' in the root module of your app and try.
Upvotes: 0
Reputation: 86730
No need to bind with ngModel
and formControlName
, Take a look here
<input class="input" type="search" #agmSearch>
<p class="even" *ngFor="let item of dayList;let i = index">
<input type="checkbox" checked="item.check" (change)="updateChkbxArray(item, $event.checked, 'selectedDays')" value="n.id" > {{item}}
</p>
Upvotes: 1