如何使用angular JS设置下拉列表控件的选定选项

问题描述 投票:135回答:8

我正在使用Angular JS,我需要使用angular JS设置下拉列表控件的选定选项。请原谅我,如果这很荒谬,但我是Angular JS的新手

这是我的html的下拉列表控件

 <select ng-required="item.id==8 && item.quantity > 0" name="posterVariants"
   ng-show="item.id==8" ng-model="item.selectedVariant" 
   ng-change="calculateServicesSubTotal(item)"
   ng-options="v.name for v in variants | filter:{type:2}">
  </select>

填充后,我得到了

 <select ng-options="v.name for v in variants | filter:{type:2}" ng-change="calculateServicesSubTotal(item)"
ng-model="item.selectedVariant" ng-show="item.id==8" name="posterVariants"
ng-required="item.id==8 &amp;&amp; item.quantity &gt; 0" class="ng-pristine ng-valid ng-valid-required">
    <option value="?" selected="selected"></option>
    <option value="0">set of 6 traits</option>
    <option value="1">5 complete sets</option>
</select>

如何设置value="0"的控件?

angularjs drop-down-menu angular-ngmodel selected
8个回答
189
投票

我希望我理解你的问题,但是ng-model指令在控件中的选定项和item.selectedVariant的值之间创建了一个双向绑定。这意味着在JavaScript中更改item.selectedVariant或更改控件中的值会更新另一个。如果item.selectedVariant的值为0,那么该项应该被选中。

如果variants是一个对象数组,则必须将item.selectedVariant设置为其中一个对象。我不知道您的范围中有哪些信息,但这是一个例子:

JS:

$scope.options = [{ name: "a", id: 1 }, { name: "b", id: 2 }];
$scope.selectedOption = $scope.options[1];

HTML:

<select data-ng-options="o.name for o in options" data-ng-model="selectedOption"></select>

这将使“b”项目被选中。


33
投票

我不知道这是否会对任何人有所帮助,但是当我遇到同样的问题时,我想分享我是如何得到解决方案的。

您可以在ng-options中使用track by属性。

假设你有:

variants:[{'id':0, name:'set of 6 traits'}, {'id':1, name:'5 complete sets'}]

你可以提到你的ng-options

ng-options="v.name for v in variants track by v.id"

希望这有助于将来的某些人。


7
投票

如果将值0分配给item.selectedVariant,则应自动选择。查看http://docs.angularjs.org/api/ng.directive:select上的样本,默认情况下只需指定$scope.color='red'即可选择红色。


7
投票

我看到这里已经写好了答案,但有时以其他形式写相同可能会有所帮助

<div ng-app ng-controller="MyCtrl">
  <select ng-model="referral.organization" ng-options="c for c in organizations"></select>
</div>

<script type='text/javascript'>
  function MyCtrl($scope) {
    $scope.organizations = ['a', 'b', 'c', 'd', 'e'];
    $scope.referral = {
      organization: $scope.organizations[2]
    };
  }
</script>

1
投票

Simple way

如果您有一个用户响应或您定义的Array / JSON,首先您需要在控制器中设置所选值,然后在html中放置相同的模型名称。我写这个例子以最简单的方式解释。 简单的例子 内部控制器:

$scope.Users = ["Suresh","Mahesh","Ramesh"]; 
$scope.selectedUser = $scope.Users[0];

你的HTML

<select data-ng-options="usr for usr in Users" data-ng-model="selectedUser">
</select>

复杂的例子 内部控制器:

$scope.JSON = {
       "ResponseObject":
           [{
               "Name": "Suresh",
               "userID": 1
           },
           {
               "Name": "Mahesh",
               "userID": 2
           }]
   };
   $scope.selectedUser = $scope.JSON.ResponseObject[0];

你的HTML

<select data-ng-options="usr.Name for usr in JSON.ResponseObject" data-ng-model="selectedUser"></select>
<h3>You selected: {{selectedUser.Name}}</h3>    

0
投票

请尝试以下方法:

JS文件

this.options = { 
        languages: [{language: 'English', lg:'en'}, {language:'German', lg:'de'}]
};
console.log(signinDetails.language);

HTML文件

<div class="form-group col-sm-6">
    <label>Preferred language</label>
    <select class="form-control" name="right" ng-model="signinDetails.language" ng-init="signinDetails.language = options.languages[0]" ng-options="l as l.language for l in options.languages"><option></option>
    </select>
</div>

0
投票

它可能很有用。绑定并不总是有效。

<select id="product" class="form-control" name="product" required
        ng-model="issue.productId"
        ng-change="getProductVersions()"
        ng-options="p.id as p.shortName for p in products">
</select>

例如。您可以从rest-service填写选项列表源模型。选择的值已知为填充列表并已设置。执行rest请求后,使用$ http list选项完成。但是没有设置选定的选项。由于不明原因AngularJS在阴影$ digest执行时没有选择绑定,因为它是shuold。我必须使用JQuery来设置选中。这很重要!阴影中的角度为ng-repeat optinos生成的attr“value”的值添加前缀。对于int,它是“数字:”。

$scope.issue.productId = productId;
function activate() {
   $http.get('/product/list')
     .then(function (response) {
       $scope.products = response.data;

       if (productId) {
           console.log("" + $("#product option").length);//for clarity                       
           $timeout(function () {
               console.log("" + $("#product option").length);//for clarity
               $('#product').val('number:'+productId);
               //$scope.issue.productId = productId;//not work at all
           }, 200);
       }
   });
}

-1
投票

这是我用于设置选定值的代码

countryList: any = [{ "value": "AF", "group": "A", "text": "Afghanistan"}, { "value": "AL", "group": "A", "text": "Albania"}, { "value": "DZ", "group": "A", "text": "Algeria"}, { "value": "AD", "group": "A", "text": "Andorra"}, { "value": "AO", "group": "A", "text": "Angola"}, { "value": "AR", "group": "A", "text": "Argentina"}, { "value": "AM", "group": "A", "text": "Armenia"}, { "value": "AW", "group": "A", "text": "Aruba"}, { "value": "AU", "group": "A", "text": "Australia"}, { "value": "AT", "group": "A", "text": "Austria"}, { "value": "AZ", "group": "A", "text": "Azerbaijan"}];
 
 
 for (var j = 0; j < countryList.length; j++) {
      //debugger
      if (countryList[j].text == "Australia") {
          console.log(countryList[j].text); 
          countryList[j].isSelected = 'selected';
      }
      }
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<label>Country</label>
<select class="custom-select col-12" id="Country" name="Country"   >
<option value="0" selected>Choose...</option>
<option *ngFor="let country of countryList" value="{{country.text}}" selected="{{country.isSelected}}"   > {{country.text}}</option>
</select>

在角度框架上尝试这个

© www.soinside.com 2019 - 2024. All rights reserved.