获取角度 JS 中的复选框数组值

get checkbox array value in angular JS

本文关键字:复选框 数组 JS 获取      更新时间:2023-09-26

我看到过一些帖子问同样的问题,但我无法让它工作。我对角度很陌生,所以我需要帮助。
我正在尝试插入带有标签的新收入。
我从服务中获取这些标签并像这样显示它们:

<label ng-repeat="tag in tags">
    <input type="checkbox" ng-model="tags_chosen" name="tags_chosen[tag]" ng-true-value="<%tag.id%>"/> <%tag.name%>
</label>

当我尝试以角度取回复选框值时,它不起作用:

 this.addIncome = function($scope) {
 var data = {
        'project_id':$scope.project_id,
        'amount':$scope.amount,
        'payment_date':$scope.payment_date,
        'tags':$scope.tag_chosen,
        'description':$scope.description,
        'type':$scope.type
    };
    return $http.post(URL.BASE_API + 'income/store',data).
    success(function(response) {
        ServicesStatus.return = response;
    }).error(function(response) {
        console.log('Service error');
    });
};

我该怎么做?

谢谢!

试试这个:

$scope.tag_chosen =[];
$scope.toggleSelection = function ( deviceId, $event ) {
   var checkbox = $event.target;
   var action=(checkbox.checked ? 'add':'remove');
    var idx = $scope.tag_chosen.indexOf( deviceId );
    // is currently selected
    if (action=='remove' && idx != -1 ) { 
        $scope.tag_chosen .splice( idx, 1 );
    }
    // is newly selected
    if (action=='add' && idx == -1 ) {
        $scope.tag_chosen.push( deviceId );
    }

和 html>>

ng-click="toggleSelection(yourcjeckbox value,$event)"