2

I am trying to create a directive by wrapping jquery autocomplete plugin somthing like this

<input class="form-control" auto-complete ui-items="list" modvar="selectedSvr" callback="myfunction"/>

I want to call whichever function I pass to callback attribute, how can i achieve this?

here's my directive

app.directive('autoComplete', function() {
    return function($scope, iElement, iAttrs) {
        iElement.autocomplete({
            source: $scope[iAttrs.uiItems],
            select: function (event,ui) {
                $scope.$apply(function () {
                    $scope[iAttrs.modvar] = ui.item.value;
                    // maybe register/call myfunction here
                })
            }
        });
    };
});
2
  • Mmm... take a look to isolated scope and & binding, check this link stackoverflow.com/questions/24640284/angular-directive-callback and on the other hand, autocomplete / type ahead like, why not using angular-ui one? (angular-ui.github.io/bootstrap/#/typeahead) Commented Aug 27, 2015 at 14:45
  • Thanks @Braulio for your answer. I checked that link in your answer but i can't use that solution since it suggests using isolated scope, which creates problem, then it can not access other attributes like ui-items etc. Commented Aug 27, 2015 at 15:46

1 Answer 1

2

I found solution here.

Now code looks like

HTML

<input class="form-control" auto-complete ui-items="searchList" modvar="selectedItem" on-callback="callme()"/>

Angular

app.directive('autoComplete', function() {
    return function ($scope, iElement, iAttrs) {
        iElement.autocomplete({
            source: $scope[iAttrs.uiItems],
            select: function (event,ui) {
                $scope.$apply(function () {
                    $scope[iAttrs.modvar] = ui.item.value;
                    $scope.$eval(iAttrs.onCallback);
                })
            }
        });
    };
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.