2017-04-01 15 views
0

AngularJsアプリケーションでmd-selectを使用して選択されたObjectからKeyValueを取得する際に問題がありますバージョン1.XX)。md-selectを使用してオブジェクトからキーの値を取得する際の問題AngularJs 1.XX

2つのドロップダウン「Department」と「Designation」があります。当初私はDepartmentをロードし、Departmentの選択に基づいて、指定は自動的に入力されます。親切にJSONオブジェクト

{ 
    "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>

を見て持っていますが、しています私はId'sを得ることができない。親切に私を助けてください。固定必要

答えて

0

二つの主なものは:

  1. あなたのコントローラで$scope.Select = ...を定義するが、ビューにng-model="select.Department"でそれを参照してみてください。 JavaScript/Angularは大文字と小文字を区別しますので、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>

関連する問題