我无法使用 Angular.js 将值动态设置到下拉列表中。这是我的代码:
<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();">
我的 Controller 端代码如下。
$scope.openSelect=function(){
$scope.subcat.value="1";
}
我在这里尝试使用按钮点击来设置值,但它抛出错误。 Here is my plunkr代码。
最佳答案
因为您还没有定义 $scope.subcat
对象,并且您正在尝试为其分配 value 属性。
$scope.subcat = {}; //define it inside controller
$scope.openSelect=function(){
$scope.subcat.value = "1";
}
Warning: as you're only setting
value
property of object,ng-options
would select option for you in dropdown. But what about otherproperty
values. If you are assumingvalue
as unique constraint & totally responsible entire record identity then its completely fine.
关于javascript - 无法使用 Angular.js 将值动态设置到下拉列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39997366/