我发现这段代码的工作原理与我想要的一样,但我需要过滤对象上的 Name 属性并在选择时显示对象属性。我 fork 了这个。 http://jsfiddle.net/sebmade/swfjT/
我的尝试...不起作用。这可能是很简单的事情。不是一直都是这样吗。感谢其他人的眼睛来解决这个问题。 https://jsfiddle.net/lnbailey/7py3rbgh/2/
<div ng-app='MyModule'>
<div ng-controller='autocompleteController'>
<input auto-complete ui-items="trans" ng-model="selected"> Name = {{selected}}
<div ng-repeat="name in names">
<div ng-if="selected == name">
{{selected.Name}}<br>
{{selected.Address}}<br>
{{selected.City}} <br>
{{selected.State}} <br>
{{selected.Zip}}
</div>
</div>
</div
function DefaultCtrl($scope) {
$scope.names = [{
'id': 1,
'Name': 'Transporter 1',
'Address': '1 Transporter Address',
'City': "Nashville",
'State': 'TN',
'Zip': "10001",
}, {
'id': 2,
'Name': 'Transporter 2',
'Address': '2 Transporter Address',
'City': "Nashville",
'State': 'TN',
'Zip': "10001",
}, {
'id': 3,
'Name': 'Transporter 3',
'Address': '3 Transporter Address',
'City': "Nashville",
'State': 'TN',
'Zip': "10001",
}, {
'id': 4,
'Name': 'Transporter 4',
'Address': '4 Transporter Address',
'City': "Nashville",
'State': 'TN',
'Zip': "10001",
}, {
'id': 5,
'Name': 'Transporter 5',
'Address': '5 Transporter Address',
'City': "Nashville",
'State': 'TN',
'Zip': "10001",
}
];
$scope.trans = [];
angular.forEach($scope.names, function(name) {
$scope.trans.push(name.first);
});
};
angular.module('MyModule', []).directive('autoComplete', function($timeout) {
return function(scope, iElement, iAttrs) {
iElement.autocomplete({
source: scope[iAttrs.uiItems],
select: function() {
$timeout(function() {
iElement.trigger('input');
}, 0);
}
});
};
});
最佳答案
您可以按照推荐的方式将 Bootstrap Typehead 与 Angular 一起使用,
演示
angular
.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap'])
.controller('MainCtrl', function($scope, $http) {
//ngModel value
$scope.selected = undefined;
//lookup values
$scope.names = [{
'id': 1,
'Name': 'Transporter 1',
'Address': '1 Transporter Address',
'City': "Nashville",
'State': 'TN',
'Zip': "10001",
}, {
'id': 2,
'Name': 'Transporter 2',
'Address': '2 Transporter Address',
'City': "Nashville",
'State': 'TN',
'Zip': "10001",
}, {
'id': 3,
'Name': 'Transporter 3',
'Address': '3 Transporter Address',
'City': "Nashville",
'State': 'TN',
'Zip': "10001",
}, {
'id': 4,
'Name': 'Transporter 4',
'Address': '4 Transporter Address',
'City': "Nashville",
'State': 'TN',
'Zip': "10001",
}, {
'id': 5,
'Name': 'Transporter 5',
'Address': '5 Transporter Address',
'City': "Nashville",
'State': 'TN',
'Zip': "10001",
}];
});
<!doctype html>
<html ng-app="ui.bootstrap.demo">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-1.3.2.js"></script>
<script src="example.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div ng-controller="MainCtrl">
<h4>Custom templates for results</h4>
<pre>Model: {{selected | json}}</pre>
<input type="text" ng-model="selected" uib-typeahead="country as country.Name for country in names | filter:{Name:$viewValue}" class="form-control" typeahead-show-hint="true" typeahead-min-length="0">
</div>
</body>
</html>
关于angularjs - 对象上的自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42769782/