Prasanjit Prakash
Prasanjit Prakash

Reputation: 529

Manipulating list values in select2 using ui-select2 directive in AngularUI

I'm trying to set the value of select2 from iside the controller. The problem is that when I use ng-options, changes in model value are not reflected in the select2 list.

html:

<div ng-controller="MyCtrl">
    <a ng-click="selected2=['B','C']">Test</a>
    <br/>
    <select ui-select2 multiple ng-model="selected2" ng-options="item for item in newArr">          
    </select>
</div>

js:

var myApp = angular.module('myApp', ['ui']);

function MyCtrl($scope) {
    $scope.newArr = ['A','B','C'];
    $scope.$watch('selected2', function(newVal,oldVal){
        console.log(newVal,oldVal);
    });
}

Here's the fiddle

http://jsfiddle.net/je_et/6yyqg/

Upvotes: 0

Views: 619

Answers (1)

conman
conman

Reputation: 128

This is working for me. Try adding a line to the controller to initialize selected2.

var myApp = angular.module('myApp', ['ui']);

function MyCtrl($scope) {
    $scope.newArr = ['A','B','C'];
    $scope.selected2 = ['A'];
    $scope.$watch('selected2', function(newVal,oldVal){
        console.log(newVal,oldVal);
    });
}

Upvotes: 1

Related Questions