最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Two way databinding in Select2 for AngularJS doesn't work - Stack Overflow

programmeradmin5浏览0评论

I am having issues with using the Select2 plugin in AngularJS. I can load the items fine, and retrieve the selected item from my ng-model, but I have issues, that the dropdown isn't updated if I update the ng-model.

In my view the code looks like this:

<select ui-select2 data-placeholder="All" id="filtersSelect" ng-model="chosenFilterItem" ng-options="item.text for item in filterItems">

In my controller I have the following code, which retrieves the items and binds it to the list:

$scope.fetchFilters = function(){
        $http.get($scope.filtersUrl).then(function(result){
            $scope.filterItems = result.data;
            $scope.chosenFilterItem = result.data[3];
            if(!$scope.$$phase) {
                $scope.$apply();
            }
        });
    }

As you can see I just try to set the 3rd item on the dropdownlist, but no item is preselected. Is there another way around preselecting a dropdown item?

I am having issues with using the Select2 plugin in AngularJS. I can load the items fine, and retrieve the selected item from my ng-model, but I have issues, that the dropdown isn't updated if I update the ng-model.

In my view the code looks like this:

<select ui-select2 data-placeholder="All" id="filtersSelect" ng-model="chosenFilterItem" ng-options="item.text for item in filterItems">

In my controller I have the following code, which retrieves the items and binds it to the list:

$scope.fetchFilters = function(){
        $http.get($scope.filtersUrl).then(function(result){
            $scope.filterItems = result.data;
            $scope.chosenFilterItem = result.data[3];
            if(!$scope.$$phase) {
                $scope.$apply();
            }
        });
    }

As you can see I just try to set the 3rd item on the dropdownlist, but no item is preselected. Is there another way around preselecting a dropdown item?

Share Improve this question edited Jun 27, 2013 at 10:48 Utopik 3,7831 gold badge22 silver badges24 bronze badges asked Jun 16, 2013 at 20:45 DofsDofs 19.4k28 gold badges79 silver badges124 bronze badges 1
  • Please not that it is remended to use the dot-notation, so keep your data in a single variable eg. data. For more info, see this Egghead.io video tutorial: goo.gl/373Ia – ndequeker Commented Jun 16, 2013 at 20:50
Add a ment  | 

2 Answers 2

Reset to default 6

Angular-ui/ui-select2 github page states:

ui-select2 is inpatible with <select ng-options>. For the best results use <option ng-repeat> instead.

So, to save yourself from headache I also remend using options with ng-repeat as in:

$scope.filterItems = [
  {id: 1, text: 'Item1'},
  {id: 2, text: 'Item2'},
  {id: 3, text: 'Item3'},
  {id: 4, text: 'Item4'}
];

<select ui-select2 placeholder="All" ng-model="chosenItem">
  <option value=""></option>
  <option ng-repeat="item in filterItems" value="{{item.id}}">{{item.text}}</option>
</select>

DEMO PLUNKER

Stewie posted the rigth way to implement Select2, but he's using "static" elements.

The difference is that angular renders select2 before having the elements called via ajax, so in this case you need to add an ui-if statement in order to add the directive only after you have data.

<select ui-select2 placeholder="All" ng-model="chosenFilterItem" **ui-if="filterItems.length>0"**>
 <option value=""></option>
 <option ng-repeat="item in filterItems" value="{{item.id}}">{{item.text}}</option>
</select>

If it doesn't work please create a jsfiddle with ajax items loading and I'll work there!

发布评论

评论列表(0)

  1. 暂无评论