2013-03-26 68 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); 
    }); 
} 
相關問題