angular bootstrap typeahead 将 ng-model 设置为对象而不是单个字段
angular bootstrap typeahead set ng-model to the object not a single field
我正在使用 angular bootstrap typeahead 指令。有没有一种方法可以 select 在我的 ng-model
中设置整个 selected 对象而不是当我使用 typeahead 时的字符串?
例如
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>
将 uib-typeahead="state.name for state in states"
替换为 uib-typeahead="state as state.name for state in states"
。这应该为你做的工作。这是一个有效的 plunker.
我正在使用 angular bootstrap typeahead 指令。有没有一种方法可以 select 在我的 ng-model
中设置整个 selected 对象而不是当我使用 typeahead 时的字符串?
例如
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>
将 uib-typeahead="state.name for state in states"
替换为 uib-typeahead="state as state.name for state in states"
。这应该为你做的工作。这是一个有效的 plunker.