gpt4 book ai didi

angularjs - Angular Bootstrap typeahead 将 ng-model 设置为对象而不是单个字段

转载 作者:行者123 更新时间:2023-12-03 06:54:06 24 4
gpt4 key购买 nike

我正在使用 Angular Bootstrap typeahead 指令。当我使用 typeahead 时,有没有办法选择要在我的 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>

最佳答案

uib-typeahead="state.name for state in states" 替换为 uib-typeahead="state as state.name for state in states"。这应该可以为你完成工作。这是一个工作plunker .

关于angularjs - Angular Bootstrap typeahead 将 ng-model 设置为对象而不是单个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34050656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com