0

I have a repeating Autocomplete control througout an MVC web app I am building. I decided to use Kendo's Angular Autocomplete tool - because we also use their calendar and dropdownlist controls (FYI - handy for large lists - as allows a search within the dropdown). And we are also using Angular.

I have got the Autocomplete working and "auto-completing" from a directive. However, when you type or select a value in the autocomplete, it is not binding the model back to the parent controller. Im not an expert on directives so I would love some help if you can! Please see this plunker which has everything in it to replicate my test! http://plnkr.co/edit/Zlw75QhmF7xkrLKsQkP8?p=preview

The directive returns this:

    return {
        restrict: 'E',
        scope: {
            bindTo: '='
        },
        template: '<input kendo-auto-complete ng-model="vm.bindTo" k-options="vm.fruitAutoComplete" style="width: 100%;"/>',
        controllerAs: 'vm',
        controller: fruitAutocompleteCtrl,
        bindToController: true
    };

And on the html I declare this:

<fruit-autocomplete bindTo="vm.selectedFruit"></fruit-autocomplete>

Bascialy, I am trying to get the value of the autocomplete directive to bind to the "vm.selectedFruit" variable on the controller. Any help is really appreciated!

Thanks in advance!

dalcam
  • 1,027
  • 11
  • 28

1 Answers1

1

Ok, i found it. I forked your plunk here. You should be able to see the code. If not, let me know :-).

First, the use of the directive attribute that should pass your data is wrong. In your html. You wrote:

From Directive <fruit-autocomplete bindTo="vm.selectedFruit"></fruit-autocomplete>

but that should be:

From Directive <fruit-autocomplete bind-to="vm.selectedFruit"></fruit-autocomplete>

In the html, directive names and their attributes always use dashes. In the code, it gets transformed to camelcase.

Then I also found an error in the directive itself. You wrote:

return {
        restrict: 'E',
        scope: {
            bindTo: '='
        },
        template: '<input kendo-auto-complete ng-model="vm.bindTo" k-options="vm.fruitAutoComplete" style="width: 100%;"/>',
        controllerAs: 'vm',
        controller: fruitAutocompleteCtrl,
        bindToController: true
    };

However, I find it easier if you use a 'local' variable for your scope binding. Also, in the template, you need to drop the 'vm.' and just bind ng-model to your 'local' scope variable, like this:

return {
        restrict: 'E',
        scope: {
            data: '=bindTo'
        },
        template: '<input kendo-auto-complete ng-model="data" k-options="vm.fruitAutoComplete" style="width: 100%;"/>',
        controllerAs: 'vm',
        controller: fruitAutocompleteCtrl,
        bindToController: true
    };

See? I made 'data' my local scope variable, and used that to bind it. As a sidenote however, if you would use a link function, you need to address your 'local' scope variable with dot notation: scope.data in my case.

Hope it helps!

Community
  • 1
  • 1
ocket-san
  • 874
  • 12
  • 22
  • hi @ocket-san, when i copied the logic to my project, it no longer worked. So i just figured out this doesn't work in the latest version of Angular (1.4.9). Do you have any ideas why? – dalcam Jan 26 '16 at 19:15
  • i ll look into it as soon as i got the time. In the meantime, could u give me some errormessages please? – ocket-san Jan 26 '16 at 20:21
  • Hi @ocket-san. Sorry - no error messages. It simply doesn't bind the value from the directive to the controller. It stops working from version 1.3.0. I looked into the version change logs on angular, nothing about directives between 1.2 and 1.3... – dalcam Jan 26 '16 at 21:49
  • Sorry - i was using an old plunkr- so forgot angular might be old! – dalcam Jan 26 '16 at 21:50
  • Hi @ocket-san, dont mind me. I went back into your plunker fresh, and changed the angular version to 1.4.9, and its working! – dalcam Jan 27 '16 at 04:26