angular bootstrap typeahead将ng-model设置为对象而不是单个字段

时间:2015-12-02 18:50:04

标签: angularjs angular-bootstrap

我正在使用angular bootstrap typeahead指令。当我使用typeahead时,有没有办法选择在ng-model中设置的整个选定对象而不是字符串?

e.g。

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>

1 个答案:

答案 0 :(得分:8)

uib-typeahead="state.name for state in states"替换为uib-typeahead="state as state.name for state in states"。这应该为你工作。这是一个有效的plunker