代码之家  ›  专栏  ›  技术社区  ›  abhishek kumar

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

  •  1
  • abhishek kumar  · 技术社区  · 9 年前

    下面是我用来填充多重选择下拉列表的视图代码

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

    我使用的控制器是

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

    现在,我想用绑定所选字段(即所选位置、设备、阶段控制器) $scope 在控制器端和我正在使用的模型中,会给出所选下拉键的值。

    3 回复  |  直到 9 年前
        1
  •  0
  •   miquelarranz    9 年前

    您可以在控制器中检索选定的值,因为您正在选择中使用ng model=“FIELD”。

    因此,如果要获取控制器中的值,可以使用:

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

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

        2
  •  0
  •   rahulbhondave    9 年前

    您可以添加ng更改事件以获取所选位置,

    <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);
         }
    });
    
        3
  •  0
  •   abhishek kumar    9 年前

    下面两行是我从同事那里得到的答案

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