0

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/

1 Answer 1

1

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);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.