Kalaiyarasi M Kalaiyarasi M - 1 year ago 86
HTML Question

how to make checkbox checked by default in angularjs

I am using a checkbox and checkbox should have checked as default value.


<div class="form-group col-sm-6" ng-class="{ 'has-error': pForm.caddress.$dirty && pForm.caddress.$error.required }">
<label class="control-label l_font" for="address">Current Address*</label>
<textarea class="form-control" type="text" name="caddress" placeholder="Current address" ng-model="caddress" ng-disabled ="!pEditMode" ng-required = "true" >
<span ng-show="pForm.caddress.$dirty && pForm.caddress.$error.required" class="help-block">Current Address is required</span>

<div class="form-group col-sm-6" ng-class="{ 'has-error': pForm.paddress.$dirty && pForm.paddress.$error.required }">
<label class="control-label l_font" for="address">Permanent Address*</label>
<input type="checkbox" ng-model="sameAddrres" ng-checked="copyAddress()" ng-disabled ="!pEditMode" />
<i class="inside">Select if Permanent address is same as Current address</i>

font-size: 12px;
<textarea class="form-control" type="text" name="paddress" placeholder="Permanent address"
ng-model="paddress" ng-disabled ="!pEditMode || sameAddrres" ng-required = "true" ></textarea>
<span ng-show="pForm.paddress.$dirty && pForm.paddress.$error.required" class="help-block">Permanent Address is required</span>

I am using a function to copy the current address value to permanent address value if check box is checked.

Controller :

$scope.copyAddress = function(){

if($scope.sameAddrres == true){

$scope.paddress = $scope.caddress;

Answer Source

The controller is not complete. Looks like you have initialized the $scope.sameAddress = false or not defined.

On check of checkbox, set the sameAddress filed to true, based on a function to check if both the values are same, when loading the controller.