简体   繁体   English

自动完成 jquery 不适用于 ngmodel 和 ngrepeat(角度)

[英]auto-complete jquery doesn't work with with ngmodel and ngrepeat (angular)

I am trying to use autocomplete function of jquery inside my angular controller and that doesn't work may be because of ng-model or ng-repeat I don't know exactly ^^我正在尝试在我的角度控制器中使用 jquery 的自动完成功能,但这不起作用可能是因为 ng-model 或 ng-repeat 我不完全知道^^

My html file contains this code我的 html 文件包含此代码

<div ng-if="data._id" data-ng-controller="AddonExclusivityCtrl" ng-init="init()">
<hr>
<label>Add an addon that is not bookable with this one:</label>
<input id="addon-search" type="text" 
ng-model="addon_filter_name" 
class="form-control" placeholder="Search for an addon...">
<br>
<div ng-show="data._embedded.exclusive_with && data._embedded.exclusive_with.length > 0">
<label>Not bookable with:</label>
<ul>
<li ng-repeat="exclusive_with in data._embedded.exclusive_with">
{{exclusive_with.description}} <a href="#" 
ng-click="removeExclusivity($event, exclusive_with)" 
title="Remove mutual exclusivity">
<span class="glyphicon glyphicon-trash"></span></a>
</li>
</ul>
</div>
</div>

And my controller contains the following code :我的控制器包含以下代码:

var lastFetchedAddonList = [];

$scope.init = function() {
    $('#addon-search').autocomplete({
        source: function( request, response ) {
            Addon.query({ name: request.term, global: null }, function(results) {
                lastFetchedAddonList = results.data;

                var suggestions = [];
                angular.forEach(results.data, function(v, i){
                   suggestions.push({ label: v.description, value: i });
                });
                response(suggestions);
            });
        },
        select: function( event, ui ) {
            event.preventDefault();
            if ($scope.data._embedded === undefined) {
                $scope.data._embedded = [];
            }

            if ($scope.data._embedded.exclusive_with === undefined) {
                $scope.data._embedded.exclusive_with = [];
            }

            var addon = lastFetchedAddonList[ui.item.value];

            var exclusiveAddon = new AddonExclusivity();
            exclusiveAddon._id = $scope.data._id;
            exclusiveAddon.exclusive_with_addon = addon.name;

            AddonExclusivity.save(exclusiveAddon, function(){
                $rootScope.addNotification('Added ' + addon.description + ' as mutually exclusive with ' + $scope.data.description);
                $scope.data._embedded.exclusive_with.push(addon);
                $('#addon-search').val('');
            });

            return false;
        }
    });
};

$scope.removeExclusivity = function($event, addon) {
    $event.preventDefault();
    AddonExclusivity.delete({ id: $scope.data._id, other_id: addon.name }, function(){
        $rootScope.addNotification('Addon ' + addon.description + ' is not anymore mutually exclusive with ' + $scope.data.description);
        $scope.data._embedded.exclusive_with.splice($scope.data._embedded.exclusive_with.indexOf(addon), 1);
    });
};

Thank you for your help in advance :)提前谢谢你的帮助 :)

Instead of using jquery autocomplete, you can use plain html to create your autocomplete.您可以使用纯 html 来创建自动完成功能,而不是使用 jquery 自动完成功能。

See this post here 在这里看到这篇文章

I have the same problem.我也有同样的问题。 The reason is angularjs repeat ng-model tag the id is always the same.原因是 angularjs 重复 ng-model 标签的 id 总是相同的。

So only the first tag with that id works fine with autocomplete.因此,只有具有该 id 的第一个标签才能使用自动完成功能正常工作。 but the second and after with same id would not work.但是具有相同 ID 的第二个和之后的将不起作用。

I am looking at angular-module angucomplete-alt angucomplete-alt我在看 angular-module angucomplete-alt angucomplete-alt

This should solve your problem.这应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM