M 卡在了 Angular Js 中。当我从下拉列表中选择特定选项时,我想在左按钮和中间按钮之间切换。
默认情况下,按钮位于中间。只有当我从下拉列表中选择“一个”时,我才想将它切换到左侧,并在其余选项中保持在中间。
这是我写的半生不熟的代码 (索引.html)
<div ng-controller="ButtonsCtrl">
<h4>Radio & Uncheckable Radio</h4>
<select ng-model="selectedItem">
<option ng-repeat="item in items" value="{{item}}">{{item}}</option>
</select>
<div class="btn-group">
<label class="btn btn-primary" ng-model="radioModel" btn-radio="'Left'">Left</label>
<label class="btn btn-primary" ng-model="radioModel" btn-radio="'Middle'">Middle</label>
</div>
Js文件如下:
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ButtonsCtrl', function($scope){ $scope.radioModel = 'Middle';$scope.items = ['one','two','three','four'];});
提前致谢!!!
最佳答案
为了实现这一点,我建议添加一个 ng-change
功能到<select>
元素,像这样:
<select ng-model="selectedItem" ng-change="changeFunction()">
此函数的作用是检查当前所选元素是否等于“一”,如果是,则设置 $scope.radioModdel
向左”。在所有其他情况下,将其设置为“中”。代码如下:
$scope.changeFunction = function(){
if(this.selectedItem == 'one')
$scope.radioModel = 'Left';
else
$scope.radioModel = 'Middle';
}
这是一个有效的 plnkr .
您可以阅读更多有关 Angular 处理选择元素的方式的信息 documentation .
谢谢你,祝你有美好的一天!
关于javascript - 用于切换使用 AngularJs 从下拉列表中选择特定选项的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32990739/