I am trying for the past 5 hours without success... Here is the code..

In View:

<input type="text" ng-model="foo" auto-complete/>Foo = {{foo}}

In controller:

    myapp.directive('autoComplete', function(autoCompleteDataService) {
    return {
        restrict: 'A',
        link: function(scope, elem, attr, ctrl) {
            elem.autocomplete({
                source: autoCompleteDataService.getSource(), //from your service
                select: function( event, ui ) {
                    scope.foo= ui.item.label;
                    scope.$apply;
                },
                change:function (event, ui) {
                    if (ui.item === null) {
                        scope.foo = null;
                    }
                },
                minLength: 2
            });
        }
    };
});

    myapp.factory('autoCompleteDataService', [function() {
    return {
        getSource: function() {
            return ['apples', 'oranges', 'bananas'];
        }
    }
}]);

Here is the issue...

The selected item is getting into the input box, but foo variable next to input box is not updating.

Where is the mistake.

Please suggest...

有帮助吗?

解决方案

Change

scope.$apply; 

to

scope.$apply(function(){
    scope.foo= ui.item.label;
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top