如何将属性绑定到选择框选择的输入字段

时间:2014-10-21 22:12:40

标签: angularjs

无法弄清楚如何使用此选择框绑定对象的属性。 plunker

<select ng-model="currentEmployee.FirstName" ng-options="employee.EmployeeId as employee.FirstName for employee in employees" ng-change="change(employee)">
  <option value="">-- Select Employee</option>
</select>
<input type="text" ng-model="currentEmployee.LastName" />


$scope.change = function(employee) {
  $scope.currentEmployee.FirstName = employee.FirstName;
   $scope.currentEmployee.LastName = employee.LastName;
};

1 个答案:

答案 0 :(得分:2)

对于这种情况,您不需要使用ng-change

ng-model设置为实际的员工对象,并将其绑定到$scope.currentEmployee

<select ng-model="currentEmployee" 
        ng-options="employee.FirstName for employee in employees">
   <option value="">-- Select Employee</option>
</select>

然后,您可以将currentEmployee绑定到<input>

<input type="text" ng-model="currentEmployee.LastName" />

哦,如果你想要你&#34; - 选择员工&#34;首先出现,在控制器中设置$scope.currentEmployee = "";

这是您修改后的plunker