如何在使用嵌套的ng-option时从下拉列表中将选定的值放入范围?

时间:2016-06-17 07:26:11

标签: javascript angularjs angularjs-scope angularjs-ng-options

以下是用于填充多个选择下拉列表的视图代码



<div ng-controller="sourceController">
  <form novalidate ng-submit="submit()">
    <div class="row">
      <div class="addNewButton">
        <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal">Add New Data Source</button>
        <div ng-include="'pages/modal.html'"></div>
      </div>
    </div>
    <div class="row">
      <div class="col-sm-3">
        Location:
        <select id="location" ng-model="location" ng-options="city for (city, facilities) in sourceSelection " style="width: 100px">
          <option value=''>Select</option>
        </select>

      </div>

      <div class="col-sm-3">
        Facility:
        <select id="facility" ng-disabled="!location" ng-model="facility" ng-options="facility for (facility, phases) in location">
          <option value=''>Select</option>
        </select>

      </div>

      <div class="col-sm-3">
        Phase :
        <select id="phase" ng-disabled="!facility" ng-model="phase" ng-options="phase for phase in facility">
          <option value=''>Select</option>
        </select>

      </div>

      <div class="col-sm-3">
        Device Type
        <select id="deviceType" ng-disabled="!phase" ng-model="deviceType">
          <option value=''>Select</option>
          <option ng-repeat="deviceType in deviceTypes" value="{{deviceType}}">{{deviceType}}</option>
        </select>
      </div>
    </div>

    <input type="submit" id="submit" value="Submit" />

  </form>
  <button onclick="myFunction()">click here</button>
  selected choices : {{location}} , {{facility}},

</div>
&#13;
&#13;
&#13;

我使用的控制器是

&#13;
&#13;
app.controller('sourceController', function($scope, $http, $rootScope) {
  $scope.sourceSelection = {
    "Chennai": {
      "siruseri": ["phase1", "phase2"],
      "chennai one": ["phase1"]
    },
    "kochi": {
      "Kochi_technopark": ["phase1"]
    }
  };
});
&#13;
&#13;
&#13;

现在我想将所选字段(即选定位置,设施,控制器中的相位)与控制器端的$scope绑定,而模型I使用那里给出了所选下拉列表的值键。

3 个答案:

答案 0 :(得分:0)

您可以在控制器中检索所选值,因为您使用的是ng-model =&#34; FIELD&#34;在你的选择中。

因此,如果您想获得控制器中的值,可以使用:

$scope.FIELD // FIELD can be location, phase, facility, etc

此变量将存储每个选择的所选选项的值。

答案 1 :(得分:0)

您可以添加ng-change事件以获取所选位置,

<select id="location" ng-change="getSelectedLocation()" ng-model="location" ng-options="city for (city, facilities) in sourceSelection " style="width: 100px"><option value=''>Select</option></select>

在你的控制器中,

app.controller('sourceController', function($scope, $http, $rootScope) {

     $scope.getSelectedLocation = function() {

        console.log($scope.location);
     }
});

答案 2 :(得分:0)

所以接下来两行就是答案,我从我的同事那里得到了

$scope.getSelectedLocation= function(){
        var temp = document.getElementById("location");
        var location = temp.options[temp.selectedIndex].value;
      console.log(location);
} ;