简体   繁体   English

无法使用 Angular.js 将值动态设置到下拉列表中

[英]Could not set value dynamically into drop down list using Angular.js

I am unable to set value dynamically into drop down list using Angular.js.我无法使用 Angular.js 将值动态设置到下拉列表中。 Here is my code:这是我的代码:

<div class="input-group bmargindiv1 col-md-12">
    <span class="input-group-addon ndrftextwidth text-right" style="width:180px">Business Name :</span>
    <select class="form-control" id="restau" ng-model="subcat" ng-options="qua.name for qua in listOfRestaurant | orderBy:'name' track by qua.value" ng-change="getDayFromSpecial('restau');">
      <option value="">Select Business Name</option>
    </select>
  </div>
  <input type="button" id="btn" value="Open" ng-click="openGallery();">
   <input type="button" id="btn1" value="select" ng-click="openSelect();">

My controller side code is given below.我的控制器端代码如下。

$scope.openSelect=function(){
     $scope.subcat.value="1";
   }

Here I am trying to set value using button click but it's throwing error.在这里,我尝试使用按钮单击设置值,但它抛出错误。 Here is my plunkr code.这是我的 plunkr代码。

Because you haven't define $scope.subcat object yet & you're trying to assign value property of it.因为您还没有定义$scope.subcat对象并且您正在尝试为其分配 value 属性。

$scope.subcat = {}; //define it inside controller
$scope.openSelect=function(){
   $scope.subcat.value = "1";
}

Forked Plunkr分叉的Plunkr

Warning: as you're only setting value property of object, ng-options would select option for you in dropdown.警告:由于您只设置对象的value属性, ng-options会在下拉列表中为您选择选项。 But what about other property values.但是其他property值呢? If you are assuming value as unique constraint & totally responsible entire record identity then its completely fine.如果您将value假设为唯一约束和完全负责的整个记录​​身份,那么它完全没问题。

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

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