AngularJS输入字段复选框ng-click不触发方法

时间:2018-07-25 12:02:04

标签: javascript angularjs

我需要通过ng-click来调用切换开关中的函数,但是 customerActiveDeactive 功能不会启动。

<a title="Active/ Deactivate" >
   <input type="checkbox" class="js-switch" ng-init="status=True" ng-model="status" ng-name="status" ng-checked="{{ customer.status|lower }}" ng-click="customerActiveDeactive({{ customer.id }})" />
</a>

$scope.customerActiveDeactive = function(id) {
        console.log("method call");
}

2 个答案:

答案 0 :(得分:2)

您以错误的方式将变量作为参数传递: 这样做:

<a title="Active/ Deactivate" >
       <input type="checkbox" class="js-switch" ng-init="status=True" ng-model="status" ng-name="status" ng-checked="{{ customer.status|lower }}" ng-click="customerActiveDeactive(customer.id )" />
    </a>

    $scope.customerActiveDeactive = function(id) {
            console.log("method call");
    }

答案 1 :(得分:1)

根据评论,由于客户id是您的django变量,只需更改此行:

ng-click="customerActiveDeactive({{ customer.id }})"

ng-click="customerActiveDeactive('{{ customer.id }}')"