0
<li class="list-group-item" ng-repeat="entity in displayedProjectRoles">
    <div>
        <strong>Role: </strong> 
        <select
            ng-model="project.role" 
            ng-options="option as option.name for option in project_role"
            ng-required="true"
            id="project_role"
            class="form-control">
        </select>
    </div>
</li>

How can I set the value of model project.role based on the value on entity.role in the ng-repeat? I tried ng-selected or even using entity.role in the ng-model but it's not working properly.

this is the data in displayed Project Roles

[
  {
    "empid": 2,
    "role": "employee"
  },
  {
    "empid": 1,
    "role": "pm"
  }
]

and in the project_role

[
  {
    "id": 1,
    "name": "employee"
  },
  {
    "id": 2,
    "name": "pm"
  }
]

So basically the value of role in displayedProjectRoles matches with the name in project_role

Ignacio Villaverde
  • 1,264
  • 1
  • 11
  • 15
N B
  • 87
  • 11

1 Answers1

0

This is a good example of angular select.

Essentially the ng-model for the select has to match the ng-option value to bind. In your example project.role would have to be exactly equal to option from your ng-options. Note this is type specific so had it been

option.id as option.name for option in project_role

and option.id == 1 then project.role == '1' will not bind

Community
  • 1
  • 1
Lee.Winter
  • 700
  • 9
  • 16