[英]Angularjs with jquery auto complete not working
我嘗試了過去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變量未更新。
錯誤在哪里。
請建議...
更改
scope.$apply;
至
scope.$apply(function(){
scope.foo= ui.item.label;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.