针对jQuery选择的插件的AngularJS指令不适用于异步加载的数据

时间:2014-12-18 02:30:39

标签: jquery angularjs angularjs-directive jquery-chosen

我正在尝试在我的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列表?

0 个答案:

没有答案