AngularJS BootstrapUI Typeahead with object & selection functionality -
i'm trying implement typeahead in angular using http://angular-ui.github.io/bootstrap/, typeahead field displays full addresses once clicked field populated postcode address. i'm trying use ng-change or ng-click this, without success..
angular.module('myapp', ['ui.bootstrap']) .controller("mainctrl", function ($scope) { $scope.selected = ''; $scope.states = [{postcode:'b1',address:'bull ring'},{postcode:'m1',address:'manchester'}]; $scope.setpcode = function(site) { $scope.selpcode = site.postcode; }; }); <div class="container"> <div ng-controller="mainctrl" class="row-fluid"> <form class="row-fluid"> <div class="container-fluid"> postcode <input type="text" ng-model="selpcode" /> typeahead <input type="text" ng-change="setpcode(site)" ng-model="selected" typeahead="state.address state in states | filter:$viewvalue" /> </div> </form> </div> </div>
any ideas?
the typeahead directive http://angular-ui.github.io/bootstrap/ very, flexible , there many ways of achieving desired functionality. i'm presenting 2 of them here.
firstly, typeahead directive uses syntax similar angularjs select directive. gives full control on displayed label , data bound model value. display address label , bind postcode selpcode
directly:
<input type="text" ng-model="selpcode" typeahead="state.postcode state.address state in states | filter:$viewvalue" typeahead-editable="false" />
the key here as
part in typeahead
expression: typeahead="state.postcode state.address state in states
also, please note i'm using typeahead-editable="false"
attribute bind model when selection made. working jsfiddle here: http://jsfiddle.net/jlupa/
another solution, if need use callback function use typeahead-on-select
attribute:
<input type="text" typeahead-on-select="setpcode($item)" ng-model="selected" typeahead="state.address state in states | filter:$viewvalue" />
it allows specify callback when match selected. working fiddle here: http://jsfiddle.net/t8bv2/
as last note: ng-change
won't work here since react on change in input while want capture selection only. ng-click
not of use either reacts on clicking on input field , not matches popup. on top of wouldn't react on selections made using keayboard.
Comments
Post a Comment