ngModel非常棒!如果您将索引指定为模型
selectedValues
<select multiple ng-model="selectedValues">
根据您的列表(
selected)构建
$watch
$scope.$watch('selected', function(nowSelected){ // reset to nothing, could use `splice` to preserve non-angular references $scope.selectedValues = []; if( ! nowSelected ){ // sometimes selected is null or undefined return; } // here's the magic angular.forEach(nowSelected, function(val){ $scope.selectedValues.push( val.id.toString() ); });});
ngModel会自动为您选择它们。
请注意,此数据绑定是单向的(
selected到UI)。如果您想使用
<select>UI来构建列表,建议您重构数据(或使用其他数据,
$watch但是这些数据可能很昂贵)。
是的,
selectedValues需要包含字符串,而不是数字。(至少对我有用:)
有关完整示例,请访问http://jsfiddle.net/JB3Un/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)