ng-model不是从ng-options中选择的

时间:2016-07-27 23:34:37

标签: javascript angularjs

<select class="form-control"
        id="projects"
        ng-model="parent_id"
        ng-options="project.id as project.groupingName for project in projects">
</select>

有一个project的{​​{1}}与gid匹配,但默认情况下仍未选中。为了证明这一点,我做了:

parent_id

1 个答案:

答案 0 :(得分:1)

我发现您的代码中存在parent_id和项目ID的差距。我为你的要求写了一些代码。如果我理解正确,如果该项目的ID等于parent_id,则要显示项目。我正确地为您更新了ng-options,因为他们应该通过&#34; id&#34;该项目。

您是否在寻找http://codepen.io/aechannaveen/pen/NABbXq之类的内容?

    <html ng-app="myApp">

<head>
    <title>
        My Angular App
    </title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js">
    </script>
    </link>
</head>

<body class="container" ng-controller="InputCtrl">

     <select class="form-control" id="projects" ng-model="project" ng-options="project as project.groupingName for project in projects track by project.id">
      </select> 
  <div ng-show="project.id === parent_id">
    Project Selected :{{ project.groupingName }}
  </div>
    </body>
</html>

并且

angular.module('myApp', []).controller('InputCtrl', ['$scope',function($scope) {
$scope.parent_id = 2;
    $scope.projects = [
   {
        "groupingName": "ABC",
        "id": 1
    }, 
    {
        "groupingName": "CDE",
        "id": 2
    } ];



}]);