0

我在从使用 AngularJs 应用程序(版本 1.XX)中的 md-select 选择的对象中获取 aValue时遇到问题。Key

我有两个下拉菜单“ Department”和“ Designation”。最初我加载了部门并根据部门选择,名称将自动填充。请查看 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"
            }
         ]
      }
   ]
}

请看一下代码

<!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>

我正在尝试显示选定项目的DepartmentIdDesignationId,但我无法获得 ID。请帮助我。

4

1 回答 1

0

需要解决的两个主要问题:

  1. 您在控制器中定义$scope.Select = ...,但尝试ng-model="select.Department"在视图中引用它。JavaScript/Angular 区分大小写,因此Select !== select请确保引用具有相同大小写的变量。

  2. <md-option>元素上使用ng-value="key"而不是value="{{key}}"当您要将对象值绑定到控件时。有了这个修复,您就不需要JSON.parse(...)在 onChange 处理程序中了。

通过这两个更改,这里是更新的代码:

    <!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>

于 2017-04-01T14:41:17.103 回答