AngularJS选择多个选项

AngularJS选择多个选项,第1张

AngularJS选择多个选项

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/



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5642126.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存