You can use simple typeahead instead of using directive, got through the code below :(您可以通过以下代码使用简单的typeahead而不是使用指令:)
HTML Code:(HTML代码:)
<input class="form-control" placeholder="Job Title / Keywords"
uib-typeahead="job.JobTitleText as job.JobTitleText for jobs in searchJob($viewValue)"
typeahead-on-select="setJob($item)"
typeahead-editable="false"
typeahead-loading="loadingJobs"
typeahead-no-results="noJobsFound"
ng-model="inSearch" required=""/>
<div>
<p ng-show="noJobsFound">No JobsFound</p>
<p ng-show="loadingJobs">Loading...</p>
</div>
Angular Code:(角度代码:)
$scope.setJob = function (job) {
//If your http response return object then write 'job.JobTitleText' or if it returns simple string then you write 'job'
$scope.inSearch = job.job.JobTitleText; //This is considering response is object
};
$scope.searchJob = function (searchTerm) {
return $http({
method: "GET",
url: "http://localhost:59836/api/Jobs/searchjobs/" + searchTerm
}).then(function Success(response) {
$scope.jobs = JSON.parse(response.data);
}, function Error(response) {
alert(response.statusText);
}).$promise;
};
That's it, this will solve your problem.(就是这样,这将解决您的问题。) Just a small suggestion which I want to give you which I am sure you must have thought is that build your back end query in such a way that your database returns the suggestions like in MySql we have query like this :(我想给您的只是一个小建议,我确定您必须想到的是,以这样的方式构建后端查询,即您的数据库返回像MySql这样的建议,我们这样查询:)
Consider you have a table by name employee where you want to put a typeahead on employee name and you have a field called 'name' in your table where you are storing employees name, so your query will be(假设您有一个按名称命名的表employee,您想在该表上输入一个雇员姓名,并且在表中有一个名为“ name”的字段,用于存储雇员的姓名,因此查询将)
SELECT * from employee WHERE lower(name) LIKE '%a%';
This query will return all the employees whose name has 'a' letter in them, So I hope your $scope.searchJob() function also returns data in this way.(此查询将返回名称中带有“ a”字母的所有雇员,因此,我希望您的$ scope.searchJob()函数也以这种方式返回数据。)
Give this a try, I hope this solves your problem.(试试看,希望这可以解决您的问题。) 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…