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

时间:2017-01-25 14:05:02

标签: javascript angularjs

我有一个组合框,我正在尝试从控制器设置所选值。我怎样才能做到这一点?

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

我试过了:

$scope.selectedControl = "Test";

控制阵列:

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

2 个答案:

答案 0 :(得分:0)

您的选项中的

只需传递值<md-option ng-repeat="control in controls" ng-value="control" ng-selected="index == 1">control</md-option>

的索引

这将选择数组中的秒值,以便您可以根据需要对其进行修改。

答案 1 :(得分:0)

问题在于您的数据数组,因为您需要从数组中选择要在选项中选择的值本身,尝试获取正确的数组或拆分数据以便更好地控制下拉选择。

工作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
}