简体   繁体   English

从控制器设置组合框选择值

[英]Set combo box selected value from the controller

I have a combo box and I am trying to set the selected value from the controller. 我有一个组合框,我正在尝试从控制器设置选定的值。 How can I do that? 我怎样才能做到这一点?

<md-select ng-model="selectedControl" ng-change="changeControl(selectedControl)" required>
    <md-option ng-repeat="control in controls" ng-value="control">{{control}}</md-option>
</md-select>

I tried: 我试过了:

$scope.selectedControl = "Test";

Control array: 控制数组:

[{"ControlId":1,"ControlColumn":"Address","ControlText":"AddressTest"},{"ControlId":2,"ControlColumn":"City_State_Zip","ControlText":"CityTest"}] 

in your options just pass the index of value <md-option ng-repeat="control in controls" ng-value="control" ng-selected="index == 1">control</md-option> 在您的选项中,只需传递值索引<md-option ng-repeat="control in controls" ng-value="control" ng-selected="index == 1">control</md-option>

this will select the scond value in array so you can modify it accoding to your requirement. 这将在数​​组中选择第二个值,因此您可以根据需要对其进行修改。

problem lies in your data array as you need to select the value itself from the array to be selected in options, try to get proper array or split the data for better control over drop down selection. 问题在于您的数据数组,因为您需要从选项中要选择的数组中选择值本身,尝试获取适当的数组或拆分数据以更好地控制下拉选择。

Working jsfiddle 工作jsfiddle

<div ng-app="myApp" ng-controller="MyCtrl">
   <div class="col-xs-12">
      <label class="col-xs-6 control-label">Type:</label>
      <div class="col-xs-6">
         <select name="type" ng-model="selectedControl" ng-dropdown required ng-change="changeControl(selectedControl)" ng-options="control.ControlText for control in controls"> 
         </select>
      </div>
   </div>
</div>

var myApp = angular.module('myApp',[]);


function MyCtrl($scope) {

   $scope.controls = [{ 'ControlId': 1, 'ControlColumn': 'Address', 'ControlText': 'AddressTest' }, { 'ControlId': 2, 'ControlColumn': 'City_State_Zip', 'ControlText': 'CityTest' }];
   $scope.selectedControl = $scope.controls[1]; // change the value from here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM