2015-12-02 4 views
6

मैं कोणीय बूटस्ट्रैप टाइपहेड निर्देश का उपयोग कर रहा हूं। जब मैं टाइपहेड का उपयोग करता हूं तो स्ट्रिंग के बजाय मेरे ng-model में सेट करने के लिए पूरी चयनित ऑब्जेक्ट का चयन करने का कोई तरीका है?ऑब्जेक्ट में कोणीय बूटस्ट्रैप टाइपहेड सेट एनजी-मॉडल एक फ़ील्ड नहीं

उदा।

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> 

उत्तर

7

बदलें 0,123,uib-typeahead="state as state.name for state in states" के साथ। यह आपके लिए काम करना चाहिए। यहां एक काम कर रहे plunker है।

+0

मुझे लगता है कि अंतिम भाग राज्यों में राज्य होना चाहिए –

+1

हां, आप सही हैं। इस तरह यह उदाहरण में है। मैं इसे जवाब में भूल गया। ध्यान देने के लिए धन्यवाद। –

+0

इस वाक्यविन्यास के लिए प्रासंगिक दस्तावेज़: https://angular-ui.github.io/bootstrap/#/typeahead और https://docs.angularjs.org/api/ng/directive/select –

संबंधित मुद्दे