javascript - How to (1) get selected value from Angular/Bootstrap dropdown and (2) show selected choice? -
i'm trying angular-ui dropdown work: http://angular-ui.github.io/bootstrap
- how pass value angular function can process it?
- how can display selected choice after user selects instead of showing "please select:"
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 chose ' + value) }; });
in html, modify code related dropdown add binding text displayed in dropdown (via ng-bind) + execute function when element in dropdown clicked (via ng-click) :
<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> then in javascript, add function executed when element in dropdown clicked:
$scope.selected = "tutorial"; $scope.settutorial = function(value) { $scope.selected = value; } with function, can:
1. value of item clicked
2. update selected choice.
Comments
Post a Comment