2015-12-02 11 views
6

Açısal önyükleme tipi otomatik yönlendirme direktifini kullanıyorum. Typeahead kullandığımda dize yerine benim ng-model içinde ayarlamak için seçilen tüm nesne seçmek için bir yolu var mı?açısal bootstrap typeahead set ng-model nesneyi tek bir alana değil

örn.

mycontroller.js

var getStates = function() { 
    // calls rest service that pulls in states.json from some angular factory 
} 
var StateCtrl = function ($scope) { 
    $scope.states = getStates(); 

    $scope.submit = function() { 
     // Should expect an Object and not the name of the state 
     console.log($scope.states); 
    } 
} 
angular.module('StateCtrl', []).controller(['$scope', StateCtrl]); 

states.json

[ 
// i don't want just state.name i want everything 
{name: Alabama, stateCode: 123, salesTax: .06}, 
{name: Arkansas, stateCode: 122, salesTax: .06}, 
{name: New York, stateCode: 120, salesTax: .10}, 
... 
] 

index.html

<input ng-model="selectedstate" uib-typeahead="state.name for state in states"> 
<!--- i want selectedState to be {{state}} and not {{state.name}} --> 
<button ng-click="submit()">submit</button> 

cevap

7

değiştirin uib-typeahead="state as state.name for state in states" ile. Bu işi sizin için yapmalı. İşte bir çalışan plunker.

+0

Sanırım son bölüm “eyaletlerde devlet” olmalıdır –

+1

Evet, haklısınız. Bu örnekte böyle. Cevabı unuttum. Fark ettiğin için teşekkürler. –

+0

Bu sözdizimi için ilgili dokümanlar: https://angular-ui.github.io/bootstrap/#/typeahead ve https://docs.angularjs.org/api/ng/directive/select –

İlgili konular