0

I want to display a list of roles in the checkbox , If a role has already been selected, the checkbox is selected .

i using this code for return the rolesId :

this.Adu.optionId=this.selectedEdit;
this.otionService.GetRolesfoOptionsID(this.Adu).subscribe((data)=>{
  data.forEach(el=>{
    this.selectRoleValue.push(el);

  })
  console.log(this.selectRoleValue)
})

this code is work and fill the this.selectRoleValue with role has already been selected But the checkbox is not selected.

and in HTML:

  <div *ngFor="let role of roles">
    <p-checkbox id="checkbox" value="{{role.id}}" [(ngModel)]="selectRoleValue" label="{{role.description}}" ></p-checkbox>
  </div>

whats the problem ? how can i solve this problem ?

9
  • We have no idea of what p-checkbox is. But if it works like a standard checkbox, then it will only be selected if selectRoleValue is true. Not if selectedRoleValue contains a role id. Learn how your directives work, and use a model that fits with how they work.
    – JB Nizet
    Commented Dec 28, 2018 at 7:48
  • @JBNizet ` p-checkbox` is Primeng Component
    – Mr Coder
    Commented Dec 28, 2018 at 7:49
  • what is el? Why are you converting selectRoleValue to string?
    – zer0
    Commented Dec 28, 2018 at 7:51
  • @zer0 el list of selected number and convert toString just for testing
    – Mr Coder
    Commented Dec 28, 2018 at 7:53
  • 1
    So they are numbers. But you're using string interpolation to pass the value to the checkbox. So it receives a string. I guess that's the problem. Use [value]="role.id" (do the same for the label while you're at it). That's the proper way to pass inputs. Note that you've posted the value of roles twice, and not the vlue of selectRoleValue.
    – JB Nizet
    Commented Dec 28, 2018 at 8:02

1 Answer 1

-1

You would push id with string format to selectRoleValue

this.selectRoleValue.push(el + '');

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.