2017-05-17 84 views
0

我是AngularJS的新手。我在我的場景中遇到了angular.copy問題。我有一個名稱,手機,州,區,城市等特定領域的表格。當用戶選擇州,相應的區將被檢索。與城市一樣。在這裏,我正在使用ng-change作爲州和地區的字段。在添加數據時,它工作正常。編輯特定記錄時,我使用angular.copy。那麼,只有像名字,手機,國家這樣獨立的領域才能完美複製。但是地區和城市不會被抄襲。請幫我解決這個問題。angular.copy不適用於依賴另一個的字段(使用ng-change時)

這是我的HTML表單。

<div ng-controller="EmpController"> 
<form name="empForm" id="empForm" ng-submit="addEmp(emps)"> 
    Employee ID: <input type="text" class="form-control" id="empid" name="empid" ng-model="emps.empid" required /><br> 

    Role: <select class="form-control" id="role" name="role" ng-model="emps.role" ng-options="roles.role as roles.role for roles in rolelist" required> 
      <option value="">Select</option> 
     </select><br> 
    State: <select class="form-control" id="state" name="state" ng-model="data.state" ng-options="states.state as states.state for states in statelist" required ng-change="getDistricts(data.state)"> 
      <option value="">Select</option> 
      </select><br> 
    District: <select class="form-control" id="district" name="district" ng-model="data.district" ng-options="districts.district as districts.district for districts in districtlist" required ng-change="getCities(data)"> 
       <option value="">Select</option> 
      </select><br> 
    City: <select class="form-control" id="city" name="city" ng-model="data.city" ng-options="cities.city as cities.city for cities in citylist" required ng-change="getAreas(data)"> 
      <option value="">Select</option> 
     </select><br> 
    Area: <select class="form-control" id="area" name="area" ng-model="data.area" ng-options="areas.area as areas.area for areas in arealist" required> 
      <option value="">Select</option> 
     </select><br> 
    <input type="submit" class="btn" value="Add"> 
</form> 
<table class="table table-striped"> 
    <thead> 
    <th>Employee ID</th> 
    <th>Role</th> 
    <th>State</th> 
    <th>District</th> 
    <th>City</th> 
    <th>Area</th> 
    <th>Action</th> 
    </thead> 
    <tbody> 
    <tr ng-repeat="emps in emplist"> 
     <td>{{emps.empid}}</td> 
     <td>{{emps.role}}</td> 
     <td>{{emps.state}}</td> 
     <td>{{emps.district}}</td> 
     <td>{{emps.city}}</td> 
     <td>{{emps.area}}</td> 
     <td> 
     <button class="btn btn-primary glyphicon glyphicon-edit" ng-click="editEmp(emps,$index);"></button> 
     <button class="btn btn-danger glyphicon glyphicon-trash" ng-click="deleteEmp(emps._id,$index);" style="margin-left:10px;" ng-if="emps.empid.toLowerCase()!='admin'"></button> 
     </td> 
    </tr> 
    </tbody> 
</table> 
</div> 

這裏是我的controller.js

app.controller("EmpController", function($scope, EmpService, RoleService, StateService, DistrictService, CityService, AreaService) { 
$scope.rolelist=RoleService.getRole(); 
$scope.emplist=EmpService.getEmp(); 
$scope.statelist=StateService.getState(); 
$scope.getDistricts=function(state) { 
    $scope.districtlist=DistrictService.getDistricts(state); 
} 
$scope.getCities=function(sds) { 
    $scope.citylist=CityService.getCities(sds); 
} 
$scope.getAreas=function(list){ 
    $scope.arealist=AreaService.getAreas(list); 
} 
$scope.editEmp=function(id,index){ 
    $scope.emps=angular.copy(id); 
    flag=index; 
} 
$scope.addEmp=function(emp){ 
    $scope.emps.pronarea=$scope.pronarealist; 
    if(!$scope.emps._id){ 
     console.log(emp); 
     EmpService.addEmp(emp); 
     $scope.emps={}; 
    } 
    else{ 
     EmpService.updateEmp(emp,flag); 
    } 
} 
$scope.deleteEmp=function(id,index){ 
    EmpService.deleteEmp(id,index); 
} 
}); 

回答

1

按我的理解,複製對象後,您將獲得國家,現在是基於狀態,得到了區,並分配到您的列表喜歡

$scope.editEmp=function(id,index){ 
$scope.emps=angular.copy(id); 
    $scope.districtlist=DistrictService.getDistricts(id.state); 
//some stuff here to get the cities based on the district you get if match then get the city from below code 
    $scope.citylist=CityService.getCities(id.city); 
flag=index; 
} 

後來分配這些值到模型

相關問題