1

我在过去的 5 个小时里都在尝试没有成功......这是代码..

在视图中:

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

在控制器中:

    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'];
        }
    }
}]);

这里是问题...

所选项目正在进入输入框,但输入框旁边的 foo 变量未更新。

哪里错了。

请建议...

4

1 回答 1

2

改变

scope.$apply; 

scope.$apply(function(){
    scope.foo= ui.item.label;
});
于 2013-09-25T10:45:11.010 回答