Java User
Java User

Reputation: 127

how to set by default value of checkbox false when it is unchecked?

I have a checkbox and i want to set value of checkbox as false when it is unchecked frst time. How to do that in angular js?

<div class="col-xs-6 col-xs-offset-4 col-sm-6 col-sm-offset-4 col-md-7 col-md-offset-3 col-lg-7 col-lg-offset-3 controls test-check">
    <input 
        type="checkbox"
        id="checkbox-1-1"
        class="regular-checkbox"
        ng-true-value="true"
        ng-false-value="false"
        ng-checked="addUser.isAllOrgSelected"
        ng-model="addUser.isAllOrgSelected"> 
    <label for="checkbox-1-1"></label>                      
    <label class="label-all">{{::'label.all'|translate}}</label>                        
</div>              

Upvotes: 1

Views: 5404

Answers (2)

Delmar Novelo
Delmar Novelo

Reputation: 1

It's a bit late for the answer, but you could try to do this:

You declare a variable of type boolean with true value, then you pass that variable to the html using an ngModel.

export class MenusComponent implements OnInit {

  seleccionado = true;

  forma: FormGroup;

  constructor(
              @Inject(FormBuilder) public fb: FormBuilder
  ) { 
    this.forma = this.fb.group({
      imagen: [null]
    });
  }
 }
                            <div *ngFor="let sucursal of listadoSucursales" class="col-sm-12 col-md-6">
                                <div class="custom-control custom-switch">
                                    <input [ngModel]="seleccionado" [ngModelOptions]="{standalone: true}" [checked]="existe(sucursal._id)" (click)="casillaSeleccionada(sucursal._id)" type="checkbox" class="custom-control-input" [id]="sucursal._id">
                                    <label class="custom-control-label" [for]="sucursal._id">{{sucursal.numeroSucursal}} - {{sucursal.nombreSucursal}} - {{sucursal.municipio}}, {{sucursal.departamento}}</label>
                                </div>
                            </div>

And so you already have the options selected. If you want to initialize them deselected, just change the value to false.

Sorry for my pity English, I'm using the google translator.

Upvotes: 0

jbe
jbe

Reputation: 1792

I have a checkbox and i want to set value of checkbox as false when it is unchecked frst time. How to do that in angular js?

that is actually the default behavior

angular.module('myApp', [])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
    <label>
        <input type="checkbox" ng-model="checked"> my checkbox
    </label> Checked: {{ checked | json}}
</div>


edit:

If you want to set the value of your model to false before anyone interacts with your checkbox, you could do so in a ngController:

angular.module('myApp', [])
.controller('ctrl', function() {
    var vm = this
    vm.checked = false
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
    <div ng-controller="ctrl as vm">
    <label>
        <input type="checkbox" ng-model="vm.checked"> my checkbox
    </label> Checked: {{ vm.checked | json}}
    </div>
</div>

Upvotes: 1

Related Questions