如何处理ng-repeater Angularjs中的单选按钮组

时间:2016-06-26 19:20:09

标签: angularjs twitter-bootstrap

我有一个页面,我需要提交所有需要退休或不退休的员工。我试图显示每个员工的员工姓名和单选按钮列表,其中包括退休和非退休选项。



<div class="body" ng-repeat="employee in EmployeesList">
   <div class="row" style="padding-left: 15px;">            
     <label>{{employee.name}}</label>     
   </div>
   <div class="row">
     <label class="radio-inline"><input name="radioGroup" type="radio" id="Retired" 
       checked="checked" ng-model="$parent.status" value="RETIRED"/><strong>RETIRED</strong></label>
     <label class="radio-inline"><input name="radioGroup" type="radio" id="NotRetiring"
       ng-model="$parent.status" value="NOTRETIRING"/><strong>NOT RETIRING</strong></label>
   </div>
</div>
<div class="footer">
   <button type="button" class="btn btn-default" ng-click="Submit()">OK</button>
   <button type="button" ng-click="cancel()">CANCEL</button>
</div>


Controller:

 $scope.EmployeesList = EmployeeService.getEmployeeList();

 $scope.Submit = function () {

        angular.forEach($scope.EmployeesList, function (employee, index) {                                                          
            var employeeid = employee.id;
            var date = new Date();
             
            var employeeResponse =
            {
                EmployeeID: employeeid,
                DateEntered: date,
                Status: $scope.status
            }
            
            EmployeeService.Submit(employeeResponse).then(function() {
                
            });
        }
});
&#13;
&#13;
&#13;

当我尝试选择每个员工的状态时,他是否已经退休,但它没有按照预期的那样做。如果我更改第二个员工的状态,那么第一个员工的状态将失去其状态。现在我需要看到&#34; RETIRED&#34;作为所有员工的默认选择,需要根据需要修改每个员工的状态。请帮帮我。

1 个答案:

答案 0 :(得分:0)

您想要记住每位员工的状态,请尝试:

<div class="body" ng-repeat="employee in EmployeesList">
   <div class="row" style="padding-left: 15px;">            
     <label>{{employee.name}}</label>     
   </div>
   <div class="row">
     <label class="radio-inline"><input name="radioGroup" type="radio" id="Retired" 
       checked="checked" ng-model="employee.status" value="RETIRED"/><strong>RETIRED</strong></label>
     <label class="radio-inline"><input name="radioGroup" type="radio" id="NotRetiring"
       ng-model="employee.status" value="NOTRETIRING"/><strong>NOT RETIRING</strong></label>
   </div>
</div>