微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用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,{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.

原文地址:https://www.jb51.cc/angularjs/240346.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐