Rahul Rahul - 4 months ago 14
AngularJS Question

Angularjs ng-model for checkbox input

I'm having a bit of trouble with

ng-model
. The function
checkshow()
is called whenever an option from the select box is selected. Depending on which buttons are selected it should call the function in the condition.

$scope.checkshow=function(item) {
alert(item);
var Name=item;
if ($scope.radioSelected === 'radiobutton1')
{ $scope.show(Name);
$scope.showfeature(Name);

}
else if($scope.radioSelected === 'radiobutton2' && $scope.checkboxed1 === 'detailchecked'){
alert("detail");
$scope.showDetails(Name);

}
else if($scope.radioSelected === 'radiobutton2' && $scope.checkboxed2 === 'comparechecked'){
alert("Surprise");
}

else{
$scope.showS(Name);
}

};


Only the
if
and
else
conditions are working, it never enters the
else if
part .

<input type="checkbox" id="chck1" disabled value="detailchecked" ng-model="checkboxed1">Detailed
<input type="checkbox" id="chck2" disabled value="comparechecked" ng-model="checkboxed2">Compare</label></td></tr>


There are 2 radiobuttions 1 and 2. I didn't write their code because conditions on them are working.

Answer

The model of a checkbox is a boolean: if the checkbox with ng-model="checkboxed1" is chceked, then $scope.checkboxed1 is set to true, otherwise it's set to false.

But you're comparing it to 'detailchecked', so that comparison will always be false.

Read the documentation: it explains how it works and has examples.