我试图让Angular-UI下拉列表正常工作: http://angular-ui.github.io/bootstrap
Plunker: http://plnkr.co/edit/1Vz8T4NMi39JpdSdXgFd
HTML:
<!doctype html>
<html ng-app="ui.bootstrap.demo">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.0.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="DropdownCtrl">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle"
type="button" id="menu1" data-toggle="dropdown" ng-click="toggled">Please Select:
<span class="caret"></span></button>
<ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
<li role="presentation" ng-repeat="choice in items">
<a role="menuitem" tabindex="-1" href="#">{{choice}}</a>
</li>
</ul>
</div>
</div>
</body>
</html>
JavaScript的:
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('DropdownCtrl', function ($scope, $log) {
$scope.items = ['one','two','three','four'];
$scope.toggled = function(value) {
alert('the value you chose was ' + value)
};
});
答案 0 :(得分:2)
在HTML中,修改与下拉列表相关的代码,以便为下拉列表中显示的文本添加绑定(通过ng-bind
)+在单击下拉列表中的元素时执行函数(通过{{1} }):
ng-click
然后在javascript中添加此函数,该函数在单击下拉列表中的元素时执行:
<div class="dropdown">
<button class="btn btn-default dropdown-toggle"
type="button" id="menu1" data-toggle="dropdown" ng-click="toggled" ng-bind='selected'>Tutorials
<span class="caret"></span></button>
<ul class="dropdown-menu" role="menu" aria-labelledby="menu1">
<li role="presentation" ng-repeat="choice in items">
<a role="menuitem" ng-click="setTutorial(choice)" tabindex="-1" href="#">{{choice}}</a>
</li>
</ul>
</div>
使用此功能,您可以:
1.获取被点击的项目的价值
2.更新所选择的选项。
答案 1 :(得分:2)
对于带有Bootstrap v4.0.0-alpha.6的Angular 4,这里有一个关于如何执行此操作的提示:
用于Bootstrap下拉控件的HTML
<div class="form-group row">
<label class="col-md-3 form-control-label">Account</label>
<div class="col-md-9">
<div class="btn-group" dropdown>
<button dropdownToggle type="button" class="btn btn-primary dropdown-toggle">
{{selectedValue}} <span class="caret"></span>
</button>
<ul dropdownMenu class="dropdown-menu" role="menu">
<li *ngFor="let account of accounts" value="{{account._id}}" (click)="selectValue(account)"
class="dropdown-item">{{account.firstname}} {{account.lastname}}
</li>
</ul>
</div>
</div>
</div>
JS组件代码
export class UserFormComponent implements OnInit {
selectedValue: string = '-- select value --';
selectedId: string;
// On-Click Method on dropdown control
selectValue(account: Account) {
this.selectedValue = account.firstname + ' ' + account.lastname;
this.selectedId = account._id;
}
}