gpt4 book ai didi

javascript - Angular UI typeahead 下拉菜单不出现

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:01:44 25 4
gpt4 key购买 nike

我正在使用 Angular UI's typeahead结合 Web Api 加载搜索结果。我的 Angular 是 1.2.2 版,bootstrap 是 3.1.0 版。我应该提到我也在使用 typescript 。

当我在搜索框中输入内容时,我希望带有建议的下拉菜单会从输入中掉下来。当我检查控制台时,我看到了我返回的数据,问题是没有显示它的下拉菜单。

这是我的 HTML + Angular Directive(指令):

<div class="input-group">
<input type="text" class="form-control" placeholder="Search" ng-model="selected"
data-typeahead="searchResult as searchResult for searchResult in search($viewValue) | filter:$viewValue" >
<div class="input-group-btn">
<button class="btn btn-default" type="submit" ng-click="search(Text)"><i class=" glyphicon glyphicon-search"></i></button>
</div>
</div>

这是位于我的 Controller 中的 js 函数:

$scope.search = function (criteria) {
controller.dataService.search($scope.employeeId, criteria, function (data) {
$scope.searchResult = data;
});

这是我的数据服务中的搜索功能。

export interface ICommonDataService extends IBaseDataService {
search(employeeId: string, criteria: string, successCallback: (data: SearchResult) => any);
}

dataservice.ts 信息:

export class DataServicBase {
public httpService: ng.IHttpService;
public serviceBase = '/services/api';

public static $inject = [
'$http'
];

constructor($http: any) {
this.httpService = $http;
}
}

export class CommonDataService extends DataServicBase implements ICommonDataService {
public serviceUrl = this.serviceBase + '/common';

search(employeeId: string, criteria: string, successCallback: (data: SearchResult) => any) {
this.httpService.get(this.serviceUrl + '/' + employeeId + '/search/' + criteria)
.success(function (data) {
successCallback(data);
});
}
}

这是 SearchResult 的样子:

// Class
export class SearchResult {
// Constructor
constructor(
public Employees: Employee[],
public Filters: Employee[],
public Projects: Project[]
) {
}
}

我得到的错误是这样的:

Error: matches is undefined
.link/getMatchesAsync/<@https://web.plank.local/Scripts/ui-bootstrap-tpls-0.10.0.js:3186
qFactory/defer/deferred.promise.then/wrappedCallback@https://web.plank.local/scripts/angular.js:10655
qFactory/defer/deferred.promise.then/wrappedCallback@https://web.plank.local/scripts/angular.js:10655
qFactory/ref/<.then/<@https://web.plank.local/scripts/angular.js:10741
$RootScopeProvider/this.$get</Scope.prototype.$eval@https://web.plank.local/scripts/angular.js:11634
$RootScopeProvider/this.$get</Scope.prototype.$digest@https://web.plank.local/scripts/angular.js:11479
$RootScopeProvider/this.$get</Scope.prototype.$apply@https://web.plank.local/scripts/angular.js:11740
textInputType/listener@https://web.plank.local/scripts/angular.js:15739
jQuery.event.dispatch@https://web.plank.local/scripts/jquery-2.1.0.js:4371
jQuery.event.add/elemData.handle@https://web.plank.local/scripts/jquery-2.1.0.js:4057
aM@https://cdn.qbaka.net/reporting.js:78

https://web.plank.local/scripts/angular.js
Line 9159

最佳答案

主要问题是您希望预先输入的数据列表不是列表(或未填充)。在我看来, Controller 中的函数 search 应该返回数据,而这只是一个 promise 。您可以尝试 返回 数据,并可以添加 limitToFilter 使其更健壮。而不是将它放在共享的 scope.searchResult 中,而这根本不会被您提前输入。

关于javascript - Angular UI typeahead 下拉菜单不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21982144/

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