AngularJS传递ng-repeat复选框值未定义

时间:2018-12-27 10:22:59

标签: javascript angularjs select checkbox undefined

这个问题已经问了,但不能解决我的问题。

我正在尝试将checkbox的值传递给 controller ,但我收到了undefined消息。 是Angularjs的初学者。

这是我的HTML代码:

<form class="form-horizontal" name="addForm" novalidate ng-submit="addData(addForm)">
    <div class="form-group">
        <div class="col-sm-3" ng-repeat="directories in formData.getMainDirectories">
            <input type="checkbox" ng-model="directories.main_directories_id.selected" value="{{directories.id}}"><span>  {{directories.name}}</span>
        </div>
    </div>
    <button type="submit" class="btn btn-primary btn-md" button-spinner="loading" ng-disabled="loading">Submit</button>
</form>

我的.js代码:

$scope.addData = function(form)
{
    $scope.errors = [];
    alert(form.main_directories_id); // alert here
    if(form.$valid)
    {
        $rootScope.loading = true;

        webServices.upload('create', $scope.formData).then(function(getData) {
            $rootScope.loading = false;
            if (getData.status == 200) {
                $sessionStorage.successmessage = getData.data.message;
                localStorage.directory = '';
                $scope.goback();
            } else if (getData.status == 401) {
                $scope.errors = utility.getError(getData.data.message);
                $scope.showerrors();
            } else {
                $rootScope.$emit("showerror", getData);
            }
        });
    }
}

$scope.getMainDirectories = function()
    {
        webServices.get('getMainDirectories').then(function(getData) 
        {
            $rootScope.loading = false;
            if (getData.status == 200) 
            {
                $scope.formData.getMainDirectories = getData.data;
            } else {
                $rootScope.$emit("showerror", getData);
            }
            //console.log(getData.data);
        });
    }

图片: enter image description here

1 个答案:

答案 0 :(得分:0)

最后我得到了解决方案

<div id="action1">
    @{Html.RenderAction("action1");}
</div>
<div id="action2">Wait for 3 seconds ...</div>

@section scripts {
    <script>
        $(() => {
            setTimeout(() => {
                $.ajax({
                    url: '/home/action2',
                    type: 'GET',
                    success: (data) => { $('#action2').html(data); },
                    error: () => { alert('Error'); }
                });
            }, 3000);
        });
    </script>
}

HTML:

$scope.addData = function(form)
{
    $scope.errors = [];

    for (var i = 0; i < $scope.formData.getMainDirectories.length; i++) {
        if ($scope.formData.getMainDirectories[i].Selected) 
        {
            $scope.formData.main_directories_id.push($scope.formData.getMainDirectories[i].id);
        }
    }

    //console.log($scope.formData.main_directories_id);

    if(form.$valid)
    {
        $rootScope.loading = true;
        webServices.upload('create', $scope.formData).then(function(getData) {
            $rootScope.loading = false;
            if (getData.status == 200) 
            {
                $sessionStorage.successmessage = getData.data.message;
                localStorage.directory = '';
                $scope.goback();
            } else if (getData.status == 401) {
                $scope.errors = utility.getError(getData.data.message);
                $scope.showerrors();
            } else {
                $rootScope.$emit("showerror", getData);
            }
        });
    }
}