0
我想知道如何調用控制器函數從bootstrap typeahead生成的下拉列表中選擇一個選項。下拉菜單中的每個項目都有一個唯一的ID。選擇選項時,我需要將它傳遞給另一個函數。從typeahead下拉菜單呼叫控制器功能
HTML:
<div class="container-fluid" ng-app="stock" ng-controller="mainCtrl as main">
<h2>Stock Search</h2>
<input type="text" ng-model="val" placeholder="Search for a stock" uib-typeahead="obj as obj.name for obj in result | filter:$viewValue | limitTo:10" ng-keydown="main.getStock(val)" typeahead-no-results="noResults" class="form-control" my-enter="main.getDetails(result)">
<div ng-show="noResults">
<i class="glyphicon glyphicon-remove"></i> No Results Found
</div>
</div>
的JavaScript:
angular.module('stock', ['ui.bootstrap'])
.config(['$qProvider', function ($qProvider) {
$qProvider.errorOnUnhandledRejections(false);
}])
.directive('myEnter', function() {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
scope.$apply(function(){
scope.$eval(attrs.myEnter);
});
event.preventDefault();
}
});
};
})
.controller('mainCtrl', function($scope, $http){
var app = this;
app.config = {
headers: {
'User-ID': 379,
'Access-Token':'eWEyOS5HbHNwQkRCVUJPX2d0UUNGUVR4Z1NKRTgzUkdCWHB2V1NCbWwtckNHWW5iS05NSjJLY0J5YU5CeU5QWFhTU3R5N1phdTctd250aW15dk5ZUFcySEt3ckpxNUdCNFhwQzYyNGVQcnlKSWlYa21Fa0xvQ0hIZ1kxZVRjaU0wJVVTRVIlMzc5',
'User-IP':'0.0.0.0',
'Agent':'agent'
}
};
$scope.result = [];
this.getStock = function(val){
$http.get("https://staging.investo2o.com/assetmanager-ws/api/v1/assets/getassets?query="+val+"&assetType=STK", app.config)
.then(function(response){
$scope.result = response.data;
return $scope.result;
});
}
this.getDetails = function(id){
console.log(id);
}
})//end of ctrl
我該怎麼辦呢?
使用angular-ui-bootstrap typeahead並按照文檔和示例進行操作 – charlietfl