2017-04-01 6 views
0

ValueKey 인 객체를 AngularJs 응용 프로그램에서 md-select를 사용하여 선택한 객체에서 가져 오는 데 문제가 있습니다. 버전 1.XX).md-select를 사용하여 개체에서 키 값을 가져 오는 중 문제가 발생했습니다. AngularJs 1.XX

두 개의 드롭 다운 "Department"과 "Designation"이 있습니다. 처음에 부서를로드하고 부서 선택에 따라 지정이 자동으로 채워집니다. 좋은 말 좋은 말

{ 
    "Status":true, 
    "Message":"", 
    "Result":[ 
     { 
     "DepartmentId":1, 
     "Name":"Bala", 
     "Designation":[ 
      { 
       "DesignationId":1, 
       "DepartmentId":1, 
       "Name":"Software Engg" 
      }, 
      { 
       "DesignationId":3, 
       "DepartmentId":1, 
       "Name":"Sr. Human Resouce" 
      }, 
      { 
       "DesignationId":2, 
       "DepartmentId":1, 
       "Name":"Sr. Software Engg" 
      } 
     ] 
     }, 
     { 
     "DepartmentId":2, 
     "Name":"Dev", 
     "Designation":[ 

     ] 
     }, 
     { 
     "DepartmentId":3, 
     "Name":"HR Team", 
     "Designation":[ 

     ] 
     }, 
     { 
     "DepartmentId":4, 
     "Name":"Sales", 
     "Designation":[ 
      { 
       "DesignationId":4, 
       "DepartmentId":4, 
       "Name":"Sr. Sales Manager" 
      } 
     ] 
     } 
    ] 
} 

는 내가 선택한 항목의 DepartmentIdDesignationId를 표시하기 위해 노력하고있어

<!DOCTYPE html> 
 
<html> 
 
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.1/angular-material.min.css"> 
 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script> 
 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular-animate.min.js"></script> 
 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular-aria.min.js"></script> 
 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular-messages.min.js"></script> 
 

 
<!-- Angular Material Library --> 
 
<script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.1/angular-material.min.js"></script> 
 
<body ng-app="myApp"> 
 

 
<div ng-controller="myCtrl"> 
 
    <p>Department:</p> 
 
    <md-select ng-model="select.Department" tabindex="9" ng-change="onChange(select.Department)"> 
 
    <md-option ng-repeat="key in loadUpData.Department" value="{{key}}">{{key.Name}}</md-option> 
 
    </md-select> 
 
    <p>Department ID:</p> 
 
    <md-input-container class="md-block"> 
 
    <input type="text" ng-model="select.Department.DepartmentId" maxlength="300" tabindex="32" /> 
 
    </md-input-container> 
 
    <br/> 
 
    <p>Designation:</p> 
 
    <md-select ng-model="select.Designation" tabindex="9"> 
 
    <md-option ng-repeat="key in designationData" value="{{key}}">{{key.Name}}</md-option> 
 
    </md-select> 
 
    <p>Designation ID:</p> 
 
    <md-input-container class="md-block"> 
 
    <input type="text" ng-model="select.Designation.DesignationId" maxlength="300" tabindex="32" /> 
 
    </md-input-container> 
 
</div> 
 

 
<script> 
 
var app = angular.module('myApp', ['ngMaterial']); 
 

 
app.controller('myCtrl', function($scope) { 
 
    $scope.loadUpData = { 
 
    Department: angular.fromJson("{" + 
 
     " \"Status\":true," + 
 
     " \"Message\":\"\"," + 
 
     " \"Result\":[" + 
 
     "  {" + 
 
     "   \"DepartmentId\":1," + 
 
     "   \"Name\":\"Bala\"," + 
 
     "   \"Designation\":[" + 
 
     "   {" + 
 
     "    \"DesignationId\":1," + 
 
     "    \"DepartmentId\":1," + 
 
     "    \"Name\":\"Software Engg\"" + 
 
     "   }," + 
 
     "   {" + 
 
     "    \"DesignationId\":3," + 
 
     "    \"DepartmentId\":1," + 
 
     "    \"Name\":\"Sr. Human Resouce\"" + 
 
     "   }," + 
 
     "   {" + 
 
     "    \"DesignationId\":2," + 
 
     "    \"DepartmentId\":1," + 
 
     "    \"Name\":\"Sr. Software Engg\"" + 
 
     "   }" + 
 
     "   ]" + 
 
     "  }," + 
 
     "  {" + 
 
     "   \"DepartmentId\":2," + 
 
     "   \"Name\":\"Dev\"," + 
 
     "   \"Designation\":[" + 
 
     "   ]" + 
 
     "  }," + 
 
     "  {" + 
 
     "   \"DepartmentId\":3," + 
 
     "   \"Name\":\"HR Team\"," + 
 
     "   \"Designation\":[" + 
 
     "   ]" + 
 
     "  }," + 
 
     "  {" + 
 
     "   \"DepartmentId\":4," + 
 
     "   \"Name\":\"Sales\"," + 
 
     "   \"Designation\":[" + 
 
     "   {" + 
 
     "    \"DesignationId\":4," + 
 
     "    \"DepartmentId\":4," + 
 
     "    \"Name\":\"Sr. Sales Manager\"" + 
 
     "   }" + 
 
     "   ]" + 
 
     "  }" + 
 
     " ]" + 
 
     "}").Result 
 
    }; 
 

 
    $scope.Select = { 
 
    Department: {}, 
 
    Designation: {} 
 
    }; 
 

 
    $scope.onChange = function(selected) { 
 
     var Designation = JSON.parse(selected).Designation; 
 
     console.log(Designation); 
 
     $scope.designationData = Designation; 
 
    }; 
 

 
}); 
 
</script> 
 
</body> 
 
</html>

코드를보고있는 JSON 객체로 모양을 가지고 있지만, 나는 이드를 얻을 수 없다. 친절하게 도와주세요. 고정 필요

답변

0

두 가지 주요 가지 :

  1. 당신은 당신의 컨트롤러에 $scope.Select = ...를 정의하지만,보기에 ng-model="select.Department"로 참조하려고합니다. 자바 스크립트/각도는 대소 문자를 구별하므로 Select !== select - 대소 문자가 같은 변수를 참조하십시오.

  2. <md-option> 요소의 경우 개체 값을 컨트롤에 바인딩하려는 경우 value="{{key}}" 대신 ng-value="key"을 사용하십시오. 이 문제로 인해 onChange 핸들러에 JSON.parse(...)이 필요하지 않습니다.

    <!DOCTYPE html> 
     
        <html> 
     
        <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.1/angular-material.min.css"> 
     
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script> 
     
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular-animate.min.js"></script> 
     
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular-aria.min.js"></script> 
     
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular-messages.min.js"></script> 
     
        
     
        <!-- Angular Material Library --> 
     
        <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.1/angular-material.min.js"></script> 
     
        <body ng-app="myApp"> 
     
        
     
        <div ng-controller="myCtrl"> 
     
         <p>Department:</p> 
     
         <md-select ng-model="select.Department" tabindex="9" ng-change="onChange(select.Department)"> 
     
         <md-option ng-repeat="key in loadUpData.Department" ng-value="key">{{key.Name}}</md-option> 
     
         </md-select> 
     
         <p>Department ID:</p> 
     
         <md-input-container class="md-block"> 
     
         <input type="text" ng-model="select.Department.DepartmentId" maxlength="300" tabindex="32" /> 
     
         </md-input-container> 
     
         <br/> 
     
         <p>Designation:</p> 
     
         <md-select ng-model="select.Designation" tabindex="9"> 
     
         <md-option ng-repeat="key in designationData" ng-value="key">{{key.Name}}</md-option> 
     
         </md-select> 
     
         <p>Designation ID:</p> 
     
         <md-input-container class="md-block"> 
     
         <input type="text" ng-model="select.Designation.DesignationId" maxlength="300" tabindex="32" /> 
     
         </md-input-container> 
     
        </div> 
     
        
     
        <script> 
     
        var app = angular.module('myApp', ['ngMaterial']); 
     
        
     
        app.controller('myCtrl', function($scope) { 
     
         $scope.loadUpData = { 
     
         Department: angular.fromJson("{" + 
     
          " \"Status\":true," + 
     
          " \"Message\":\"\"," + 
     
          " \"Result\":[" + 
     
          "  {" + 
     
          "   \"DepartmentId\":1," + 
     
          "   \"Name\":\"Bala\"," + 
     
          "   \"Designation\":[" + 
     
          "   {" + 
     
          "    \"DesignationId\":1," + 
     
          "    \"DepartmentId\":1," + 
     
          "    \"Name\":\"Software Engg\"" + 
     
          "   }," + 
     
          "   {" + 
     
          "    \"DesignationId\":3," + 
     
          "    \"DepartmentId\":1," + 
     
          "    \"Name\":\"Sr. Human Resouce\"" + 
     
          "   }," + 
     
          "   {" + 
     
          "    \"DesignationId\":2," + 
     
          "    \"DepartmentId\":1," + 
     
          "    \"Name\":\"Sr. Software Engg\"" + 
     
          "   }" + 
     
          "   ]" + 
     
          "  }," + 
     
          "  {" + 
     
          "   \"DepartmentId\":2," + 
     
          "   \"Name\":\"Dev\"," + 
     
          "   \"Designation\":[" + 
     
          "   ]" + 
     
          "  }," + 
     
          "  {" + 
     
          "   \"DepartmentId\":3," + 
     
          "   \"Name\":\"HR Team\"," + 
     
          "   \"Designation\":[" + 
     
          "   ]" + 
     
          "  }," + 
     
          "  {" + 
     
          "   \"DepartmentId\":4," + 
     
          "   \"Name\":\"Sales\"," + 
     
          "   \"Designation\":[" + 
     
          "   {" + 
     
          "    \"DesignationId\":4," + 
     
          "    \"DepartmentId\":4," + 
     
          "    \"Name\":\"Sr. Sales Manager\"" + 
     
          "   }" + 
     
          "   ]" + 
     
          "  }" + 
     
          " ]" + 
     
          "}").Result 
     
         }; 
     
        
     
         $scope.select = { 
     
         Department: {}, 
     
         Designation: {} 
     
         }; 
     
        
     
         $scope.onChange = function(selected) { 
     
          var Designation = selected.Designation; 
     
          console.log(Designation); 
     
          $scope.designationData = Designation; 
     
         }; 
     
        
     
        }); 
     
        </script> 
     
        </body> 
     
        </html>

    : 여기에 두 가지 변화와

업데이트 된 코드입니다