0

我在我的angularjs項目中使用了typeahead。當使用打字幫助選擇的項目時,文本值不會顯示

我哈瓦這個值與預輸入屬性顯示在文本框:

$scope.cities = [{id:1,address:'Berlin'}, 
        {id:2,address:'Bonn'}, 
        {id:3,address:'London'}, 
        {id:4,address:'Miami'}]; 

這裏是預輸入屬性的文本框中輸入:

<input type="text" ng-model="selected" typeahead="state.abbreviation as state.name for state in states | filter:$viewValue | limitTo:8"> 

但問題是,當選擇項目顯示的值的編號而不是地址。

這裏是planker

我想將地址顯示在文本框中並將id值保存在selected變量中。如何解決問題?

+0

看到這個答案http://stackoverflow.com/questions/15930339/how-to-tie-angular-uis- typeahead-with-a-server-via-http-for-server-side-optimi –

回答

2

試試這個(運行的代碼片段):

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']); 
 
angular.module('ui.bootstrap.demo').controller('TypeaheadCtrl', function($scope, $http) { 
 

 
    $scope.cities = [{id:1, address:'Berlin'}, 
 
\t \t \t \t \t \t \t {id:2,address:'Bonn'}, 
 
       {id:3,address:'London'}, 
 
       {id:4,address:'Miami'}]; 
 
       
 
});
<!doctype html> 
 
<html ng-app="ui.bootstrap.demo"> 
 
    <head> 
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script> 
 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.js"></script> 
 
    <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.3.2.js"></script> 
 
    <script src="example.js"></script> 
 
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"> 
 
    </head> 
 
    <body> 
 

 
<style> 
 
    .typeahead-demo .custom-popup-wrapper { 
 
    position: absolute; 
 
    top: 100%; 
 
    left: 0; 
 
    z-index: 1000; 
 
    display: none; 
 
    background-color: #f9f9f9; 
 
    } 
 

 
    .typeahead-demo .custom-popup-wrapper > .message { 
 
    padding: 10px 20px; 
 
    border-bottom: 1px solid #ddd; 
 
    color: #868686; 
 
    } 
 

 
    .typeahead-demo .custom-popup-wrapper > .dropdown-menu { 
 
    position: static; 
 
    float: none; 
 
    display: block; 
 
    min-width: 160px; 
 
    background-color: transparent; 
 
    border: none; 
 
    border-radius: 0; 
 
    box-shadow: none; 
 
    } 
 
</style> 
 

 
<div class='container-fluid typeahead-demo' ng-controller="TypeaheadCtrl"> 
 

 
    <h4>Custom templates for results</h4> 
 
    <pre> Id: {{city.id}}</pre> 
 
    <pre>Model: {{city | json}}</pre> 
 
    
 
    <input type="text" 
 
    ng-model="city" 
 
    placeholder="Custom template" 
 
    
 
    uib-typeahead="city as city.address for city in cities | filter:$viewValue" 
 
    class="form-control" 
 
    typeahead-show-hint="true" 
 
    typeahead-min-length="0"/> 
 
    
 
    </div> 
 
    </body> 
 
</html>

+0

這不是你正在尋找的解決方案嗎?! – Bettimms

+0

是的,非常感謝! – Michael

+0

請注意,它只是刪除收到答案的-1票!謝謝 – Bettimms

相關問題