2013-03-26 40 views
0

我试图从控制器中设置select2的值。问题是,当我使用ng选项时,模型值的更改不会反映在select2列表中。在AngularUI中使用ui-select2指令处理select2中的列表值

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); 
    }); 
} 

这里的小提琴

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

回答

1

这是为我工作。尝试添加一行到控制器初始化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); 
    }); 
}