简体   繁体   English

在AngularJS中选中复选框后如何打印天气?

[英]How to print weather when the checkbox is checked in AngularJS?

I am creating a web app with angularJS. 我正在使用angularJS创建一个Web应用程序。 Here I have a checkbox which looks like this: 这里有一个复选框,看起来像这样:

<input type="checkbox" ng-click="clickcheck()" ng-model="checkall" />

In my controller I have a function for checkbox check: 在我的控制器中,我有一个复选框检查功能:

$scope.clickcheck = function() {
    console.log(/* print wether if my checkbox is checked or not on checkbox click */);
}

What do I need to do here? 我在这里需要做什么? I just want to print whether when my checkbox is checked. 我只想打印是否在选中我的复选框时。

Your ng-model="checkall" already does the job for you: 您的ng-model="checkall"已经为您完成了这项工作:

$scope.clickcheck = function() {
    if(checkall) { 
        console.log('Checkbox is checked!'); //Print weather here
    } else {
        console.log('Checkbox is not checked');
    }
}

Use ngChange instead of ngClick and as you have already attachec ngModel ie checkall , it will evaluate to true when checkbox is checked . 使用ngChange代替ngClick ,当你已经attachec ngModelcheckall ,它会评估为true时复选框被checked

<input type="checkbox" ng-change="clickcheck()" ng-model="checkall" />

In controller 在控制器中

$scope.clickchech=function(){
    console.log($scope.checkall ? 'checked' : 'unchecked');
}

复选框的值在$scope.checkall变量中

console.log($scope.checkall);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM