AngularJs绑定到select

时间:2016-03-28 13:41:56

标签: javascript angularjs

我使用了角度1.5.2而我正试图绑定,我的select就像这样:

<label>Role</label>
<div class="form-group">
    <div>
        <select id="role" class="form-control" name="role" >
            <option value="1" ng-model="employeeCtrl.employee.RoleId" name="role" id="intern">Stagiair</option>
            <option value="3" ng-model="employeeCtrl.employee.RoleId" name="role" id="employee">Werknemer</option>
            <option value="4" ng-model="employeeCtrl.employee.RoleId" name="role" id="companyadmin">Bedrijfadmin</option>
        </select>
    </div>
</div>

但它没有用。当用户具有角色4时,它仍显示1?

2 个答案:

答案 0 :(得分:2)

您应该在ng-model="employeeCtrl.employee.RoleId"代码中使用select而不是option代码

   <select id="role" class="form-control" ng-model="employeeCtrl.employee.RoleId" >
        <option value="1" name="role" id="intern">Stagiair</option>
        <option value="3" name="role" id="employee">Werknemer</option>
        <option value="4" name="role" id="companyadmin">Bedrijfadmin</option>
    </select>

请参阅PLUNKER DEMO

答案 1 :(得分:1)

ngModel放在select元素上:

<select id="role" class="form-control" name="role" ng-model="employeeCtrl.employee.RoleId">
    <option value="1" name="role" id="intern">Stagiair</option>
    <option value="3" name="role" id="employee">Werknemer</option>
    <option value="4" name="role" id="companyadmin">Bedrijfadmin</option>
</select>