3

我的情况如下:

我有一个搜索字段,用户在其中键入文本,并且网络服务尝试根据迄今为止输入的信息来建议值。

我的问题是webservice有时需要2-3秒才能生成数据,而typeahead list没有及时填写。

 <form data-ng-submit="selectItem(message);">
    <input type="text" data-ng-model="message" data-ng-change="handleChange(message);" data-typeahead-wait-ms="500" data-typeahead="o for o in searchOptions | filter:$viewValue | limitTo:8">
    <input type="submit" style="height: 0px; width: 0px; border: none; padding: 0px;" data-hidefocus="true" />
</form>

我可以将其更改typeahead-wait-ms为 3000,但这会使其他请求非常慢(请求通常为 100-150 毫秒)。我想要做的是强制 angularjs 在将数据保存在$scope.searchOptions. 有没有办法做到这一点?

我尝试使用ng-show(隐藏和显示),尝试更改$scope.message(添加和删除空间),到目前为止没有任何东西强制重新显示。

更新:

javascript部分如下(有一个延迟计数器,仅当搜索字段自500毫秒以来未更新时才调用triggerSearch,为简单起见,我将其删除):

var triggerSearch = function() {
    searchService.setString($scope.message); // this gives the search string to service
    $injector.get('$rootScope').$broadcast('startSearch');
};

然后在另一个控制器中收听此消息:

$scope.$on('startSearch', function() {
    // here there is a $http.get which gets all the data after reading search string
    searchService.setOptions(selectionlist); // this gives back the result
    $injector.get('$rootScope').$broadcast('updatedOptions'); // calls the original controller
}

然后在原来的控制器中

$scope.$on('updatedOptions', function() {
    $scope.searchOptions = searchService.getOptions(); // reading the list from service
    $scope.$apply(); // tried this - didnt work.
});

问题是,当我在页面上的普通数据中添加服务结果时,它会在数据可用时正确显示,即 2 秒后。但是,预输入列表不会更新。

更新 2

只是为了更具体地说明最后一句话:

<form data-ng-submit="selectItem(message);">
    <input type="text" data-ng-model="message" data-ng-change="handleChange(message);" data-typeahead-wait-ms="500" data-typeahead="o for o in searchOptions | filter:$viewValue | limitTo:8">
    <input type="submit" style="height: 0px; width: 0px; border: none; padding: 0px;" data-hidefocus="true" />
</form>
{{searchOptions}}

如果我添加最后一行,则在服务完成填充此数据时会正确更新。只是没有刷新的预输入。所以在上面的例子中,我开始输入搜索子字符串,当搜索完成时,{{searchOptions}} 会正确更新搜索结果。

问题是:有没有办法强制重新生成输入字段,以便将带有新数据的 typeahead 标签考虑在内?

4

0 回答 0