lundi 9 octobre 2017

Angular 4 check multiple checkboxes

I have a form with User roles displayed as multiple checkboxes:

<div *ngFor="let role of roles">
            <label for="role_">
              <input type="checkbox" ngModel name="roles" id="role_" value="">  &nbsp;&nbsp;
            </label>
          </div>

now i want to set multiple check boxes using form.setValue, my user object loaded from server looks like this

{
"id":13,
"name":"Wasif Khalil",
"email":"wk@wasiff.com",
"created_at":"2017-10-07 10:43:17",
"updated_at":"2017-10-09 07:45:34",
"api_token":"LKVCGPGnXZ3LyiCnyiTAg8XTpck6xWlVkeoMBgtoYZWoAOy4b5epNqMz7KG7",
"roles":[
     {"id":2,"name":"admin","description":"A Manager User","created_at":"2017-10-07 10:43:17","updated_at":"2017-10-07 10:43:17","pivot":{"user_id":"13","role_id":"2","created_at":"2017-10-07 10:43:17","updated_at":"2017-10-07 10:43:17"}
     },
     {"id":1,"name":"HQ","description":"A Employee User","created_at":"2017-10-07 10:43:17","updated_at":"2017-10-07 10:43:17","pivot":{"user_id":"13","role_id":"1","created_at":null,"updated_at":null}
     }
   ]
}

after loading user object form server im setting values like this:

this.form.setValue({
                        name: user.name,
                        email: user.email,
                        password:"",
                        confirm_password:"",
                        roles: [1]
                    });

can someone help me check the checkboxes with the loaded user roles object. Thanks in advance




Aucun commentaire:

Enregistrer un commentaire