【问题标题】:Passing a parameter in ng-click function在 ng-click 函数中传递参数
【发布时间】:2018-07-21 00:52:17
【问题描述】:

我正在尝试将用户电子邮件订阅状态的数据传递给 API,该状态为“Y”/“N”。

在控制器代码中console.log(a.checked) 未定义。但在常规 javascript 中,输入元素 type="checkbox" 的 onclick 事件具有 this.checked 以相应地响应为真或假。为什么在 AngularJS 中没有发生这种情况?

我的带有角度指令 ng-click 的 html 代码:

<label for="mail_sub">E-mail subscription</label>
<input id="mail_sub" type="checkbox" name=checkbox ng-click="mailsubscription(this)">

控制器代码:

.controller("registerCtrl", function($scope, $state, userProcess) {
    $scope.mailsubscription = function(a) {
        console.log(a);
        console.log(a.checked); // console output is: "undefined"
        signupinfo = {};
        if (a.checked) {
            signupinfo.u_mail_subscription = 'Y';
        } else {
            signupinfo.u_mail_subscription = 'N';
        }
        console.log(signupinfo);

    };
    /*$scope.registerProcess = function(signupinfo){
                console.log(signupinfo);
                userProcess.signup(signupinfo).sucess(function(response){

                    if(response.status){


                    }
                })

            };*/
});

【问题讨论】:

  • 你在哪里为这个输入指定模型 ng-model 在哪里?

标签: angularjs checkbox input this


【解决方案1】:

在您的范围内没有定义检查。你必须这样做:

$scope.checked = false
$scope.mailsubscription = function () {
    $scope.checked = !$scope.checked;
    console.log($scope.checked)
}; 

或者您可以在模板中使用 ngModel 指令

<input id="mail_sub" type="checkbox" name=checkbox ng-click="mailsubscription(this)" ngModel="checked">

如果你这样做,你不需要切换你自己检查的变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    相关资源
    最近更新 更多