使用AngularJS从按钮单击下拉列表中选择所有选项

时间:2015-10-12 07:45:36

标签: html angularjs

我想从<select multiple ng-model="selectedToPostalCodes" ng-options="items.postalcode for items in toPostalCodes"> <option style="display:none;"></option> </select> <input type="button" value="Select All" ng-click="selectall()"> 上的HTML下拉列表中选择所有值。这是我的HTML代码

selectall()

所以我希望实现let cell = self.chatTableView.dequeueReusableCellWithIdentifier(msgCellIdentifier) as! ChatMsgCell cell.nickName.text = "Li" cell.portrait!.image = UIImage(named: "wk.png") cell.msgContent.text = items[indexPath.row].msgContent as String cell.bubble.image = UIImage(named: "left_bubble.png")!.resizableImageWithCapInsets(edgeInsets) cell.msgContent.scrollEnabled = false 函数。

由于

2 个答案:

答案 0 :(得分:1)

您可以尝试这种方式:: Fiddle

fiddleApp.controller('Main', ['$scope', function($scope) {
 $scope.toPostalCodes = [
        {postalcode:1, name:"Japan"},
        {postalcode:4, name:"USA"}
    ]; 
 $scope.selectedToPostalCodes = [];  
$scope.selectall = function(){        
     $scope.selectedToPostalCodes = []; 
     angular.forEach($scope.toPostalCodes, function(item){
         $scope.selectedToPostalCodes.push( item.postalcode);
     }); 
 }   
}]);

答案 1 :(得分:0)

您可以尝试这样:

$scope.selectall = function() {
    var allOptions = [];
    // First get all codes as list
    angular.forEach($scope.toPostalCodes, function(item) {
        allOptions.push(item.postalcode);
    });

    // And then assign the values to the scope model
    $scope.selectedToPostalCodes = allOptions;
};