문제

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