加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何创建AngularJS jQueryUI自动完成指令

发布时间:2020-12-17 17:31:40 所属栏目:安全 来源:网络整理
导读:我正在尝试创建一个使用jQueryUI的自动完成小部件的自定义指令.我希望这是尽可能的声明.这是所需的标记: div autocomplete ng-model="employeeId" url="/api/EmployeeFinder" label="{{firstName}} {{surname}}" value="id" //div 因此,在上面的示例中,我希
我正在尝试创建一个使用jQueryUI的自动完成小部件的自定义指令.我希望这是尽可能的声明.这是所需的标记:

<div>
    <autocomplete ng-model="employeeId" url="/api/EmployeeFinder" label="{{firstName}} {{surname}}" value="id" />
</div>

因此,在上面的示例中,我希望指令对指定的url执行AJAX调用,并在返回数据时,显示从文本框中的结果中的表达式计算的值,并将id属性设置为employeeId.这是我对该指令的尝试.

app.directive('autocomplete',function ($http) {
    return {
        restrict: 'E',replace: true,template: '<input type="text" />',require: 'ngModel',link: function (scope,elem,attrs,ctrl) {
            elem.autocomplete({
                source: function (request,response) {
                    $http({
                    url: attrs.url,method: 'GET',params: { term: request.term }
                })
                .then(function (data) {
                    response($.map(data,function (item) {
                        var result = {};

                        result.label = item[attrs.label];
                        result.value = item[attrs.value];

                        return result;
                    }))
                });
                },select: function (event,ui) {                    
                    ctrl.$setViewValue(elem.val(ui.item.label));                    

                    return false;
                }
            });
        }
    }    
});

所以,我有两个问题 – 如何评估label属性中的表达式以及如何将value属性中的属性设置为我的作用域上的ngModel.

解决方法

这是我更新的指令

(function () {
'use strict';

angular
    .module('app')
    .directive('myAutocomplete',myAutocomplete);

myAutocomplete.$inject = ['$http','$interpolate','$parse'];
function myAutocomplete($http,$interpolate,$parse) {

    // Usage:

    //  For a simple array of items
    //  <input type="text" class="form-control" my-autocomplete url="/some/url" ng-model="criteria.employeeNumber"  />

    //  For a simple array of items,with option to allow custom entries
    //  <input type="text" class="form-control" my-autocomplete url="/some/url" allow-custom-entry="true" ng-model="criteria.employeeNumber"  />

    //  For an array of objects,the label attribute accepts an expression.  NgModel is set to the selected object.
    //  <input type="text" class="form-control" my-autocomplete url="/some/url" label="{{lastName}},{{firstName}} ({{username}})" ng-model="criteria.employeeNumber"  />

    //  Setting the value attribute will set the value of NgModel to be the property of the selected object.
    //  <input type="text" class="form-control" my-autocomplete url="/some/url" label="{{lastName}},{{firstName}} ({{username}})" value="id" ng-model="criteria.employeeNumber"  />

    var directive = {            
        restrict: 'A',compile: compile
    };

    return directive;

    function compile(elem,attrs) {
        var modelAccessor = $parse(attrs.ngModel),labelExpression = attrs.label;

        return function (scope,element,attrs) {
            var
                mappedItems = null,allowCustomEntry = attrs.allowCustomEntry || false;

            element.autocomplete({
                source: function (request,response) {
                    $http({
                        url: attrs.url,params: { term: request.term }
                    })
                    .success(function (data) {
                        mappedItems = $.map(data,function (item) {
                            var result = {};

                            if (typeof item === 'string') {
                                result.label = item;
                                result.value = item;

                                return result;
                            }

                            result.label = $interpolate(labelExpression)(item);

                            if (attrs.value) {
                                result.value = item[attrs.value];
                            }
                            else {
                                result.value = item;
                            }

                            return result;
                        });

                        return response(mappedItems);
                    });
                },ui) {
                    scope.$apply(function (scope) {
                        modelAccessor.assign(scope,ui.item.value);
                    });

                    if (attrs.onSelect) {
                        scope.$apply(attrs.onSelect);
                    }

                    element.val(ui.item.label);

                    event.preventDefault();
                },change: function () {
                    var
                        currentValue = element.val(),matchingItem = null;

                    if (allowCustomEntry) {
                        return;
                    }

                    if (mappedItems) {
                        for (var i = 0; i < mappedItems.length; i++) {
                            if (mappedItems[i].label === currentValue) {
                                matchingItem = mappedItems[i].label;
                                break;
                            }
                        }
                    }

                    if (!matchingItem) {
                        scope.$apply(function (scope) {
                            modelAccessor.assign(scope,null);
                        });
                    }
                }
            });
        };
    }
}
})();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读