我正在尝试在我的AngularJS应用程序中为jQuery chosen plugin设置一个指令,但是,插件没有更新以显示我使用AJAX / $ http加载的项目列表。
我的问题的演示可以是seen on plunker。
以下是相关代码:
HTML:
<select
style="width: 100%;"
ng-chosen
multiple
ng-model="selectedItem"
ng-options="item.name for item in items">
</select>
JS:
angular.module('myApp', [])
.controller('myController', function($scope, $http) {
$scope.selectedItems = {};
$scope.items = undefined;
$http.get('items.json').then(function(response) {
$scope.items = response.data;
}, function() {
// Error ...
});
})
.directive("ngChosen", function() {
return {
require: 'ngModel',
restrict: 'A',
link: function(scope, elem, attrs, ngModel) {
scope.$watch(attrs.ngModel, function(newValue) {
// Trigger chosen update
$(elem).trigger("chosen:updated");
});
// Init chosen
$(elem).chosen();
}
};
});
我如何才能使这个工作正常,chosen
元素显示我的items
列表?