我正在尝试在 Angular 指令中实现 jquery 的自动完成功能。我收到的源数据来自 websocket 响应。它不起作用,我认为响应延迟导致了这里的问题。
如果有人能对下面的代码有所了解,我将不胜感激。是否有任何优雅的技术可以使用某种请求/响应或承诺来实现这一目标?
app.directive('autoComplete', function($rootScope, locationAutoCompleteService, $timeout, $http, programLocationModel ) {
return {
restrict: 'A',
scope: {
serviceType: '@serviceType'
},
link: function(scope, elem, attr, ctrl) {
var autoItem = [];
scope.change = function () {
locationAutoCompleteService.unSubscribe();
var service = locationAutoCompleteService.getServiceDefinition();
service.filters.pattern = scope.inputVal;
locationAutoCompleteService.subscribe();
};
scope.$on('myData', function(event, message){
if ( message !== null && message.results !== null) {
autoItem = [];
for ( var i = 0; i < message.results.length; i++) {
autoItem.push({ label: message.results[i].name, id: message.results[i].id });
}
}
});
elem.autocomplete({
source: autoItem,
select: function( event, ui ) {
$timeout(function() {
elem.trigger('input');
}, 0);
}
});
}
};
});