Don't know how to check if a input is checked in Angular 1.x?

Don't know how to check if a input is checked in Angular 1.x?
0

#1

Link

I want to check if a input is checked, but when I try to console log the input checked to see if it returns true or false, but it returns undefined. Maybe it’s better if i do it through the DOM, but I wanna see if it can be done through Angular.


#2
> Controller :

> var app = angular.module('myApp', []);

> function mainCtrl ($scope) {
>   $scope.list = {
>     item1 : {
>       checked : false
>     },
>      item2 : {
>       checked : false
>     },
>      item3 : {
>       checked : false
>     },
>      item4 : {
>       checked : false
>     }
>   };
>   console.log($scope.list.item1.checked);

> }
> mainCtrl.$inject = ['$scope'];
> app.controller('mainCtrl', mainCtrl);

View :

 <li><input type="checkbox" id="item_1" ng-model="list.item1.checked"><label for="item_1">Kruh</label></li>
    <li><input type="checkbox" id="item_2" ng-checked="list.item2.checked"><label for="item_2">Maslac</label></li>
    <li><input type="checkbox" id="item_3" ng-checked="list.item3.checked"><label for="item_3">Mlijeko</label></li>
    <li><input type="checkbox" id="item_4" ng-checked="list.item4.checked"><label for="item_4">Čokolada</label></li>
    {{ list.item1.checked }}

#3

Hi,
There are 2 directives that can work, both ng-model and ng-checked. This is one way to illustrate possible use of them.
ng-checked is used in relative way, that means if you want to check checkbox based on some variable that exists on scope. You can combine two depening on your needs.
Useful link : https://docs.angularjs.org/api/ng/directive/ngChecked
In this code I printed out below in the view state of first checkbox to see that is really working, flashing state in controller is not quite showing true value because page is constantly refreshed.