How to bind dynamic Check boxes value using ng-model?

后端 未结 3 778
生来不讨喜
生来不讨喜 2021-01-24 09:07

I want to put the values of dynamic checkboxes (not boolean true and false) in the form of array using \'ng-model\' in a similar way as is done using \'name\' attribute. This ar

3条回答
  •  [愿得一人]
    2021-01-24 09:21

    You have to bind each input to a different value. Currently all of them are bound to the field operations in the scope via ng-model="operations".

    I suggest you create an array operations in your controller like this:

    $scope.operations = new Array($scope.operations_publish.length);
    

    Then you can bind the checkboxes to the respective index in this array:

    
        
        {{operation}}
    
    

    This will give you an array with true at all checked indexes. If you then want the selected values as strings in an array, you can collect them like this:

    function getSelected() {
        return $scope.operations_publish.filter(function (x,i) {
            return $scope.operations[i]
        });
    }
    

    Check this fiddle for the complete code.

提交回复
热议问题