gpt4 book ai didi

javascript - Angularjs jquery UI 自动完成

转载 作者:可可西里 更新时间:2023-11-01 02:37:55 25 4
gpt4 key购买 nike

我正在尝试在 Angular 指令中实现 jquery 的自动完成功能。我收到的源数据来自 websocket 响应。它不起作用,我认为响应延迟是导致这里问题的原因。

如果有人可以阐明下面的代码,我将不胜感激。是否有任何优雅的技术可以使用某种请求/响应或 promise 来实现这一点?

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);

}
});
}
};
});

最佳答案

您总是可以利用这些人所做的工作:http://angular-ui.github.io/bootstrap

-向下滚动以提前输入-

这是一个 Plunkr:http://plnkr.co/edit/9zsrvLLfH8hKGwmIeZVv?p=preview

这是一些标记:

HTML

<div class='container-fluid' ng-controller="TypeaheadCtrl">
<pre>Model: {{selected| json}}</pre>
<input type="text" ng-model="selected" typeahead="state for state in states | filter:$viewValue">
</div>

JS

function TypeaheadCtrl($scope) {

$scope.selected = undefined;
$scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'];
}

更新

看来我关注的问题不对。尝试将自动完成调用移动到 $on 处理程序中。

像这样:

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);
}
});
}
});
}
};
});

关于javascript - Angularjs jquery UI 自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17432258/

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