Samantha J T Star
Samantha J T Star

Reputation: 32788

Is there a way to set the initial value of a checkbox to false?

I have:

<input ng-model="answer.response" type="checkbox" />

When data is fetched from the server there will initially be no value for answer.response.

Is there a way I can make this default to false?

Upvotes: 1

Views: 53

Answers (2)

Michael Kang
Michael Kang

Reputation: 52847

The checkbox is bound to the model answer.response.

If you want to default it to false, then set answer.response to false in your controller.

app.controller('ctrl', function($scope) { 
    $scope.answer = { response: false };
});

HTML:

<div ng-controller='ctrl'>
  <input ng-model="answer.response" type="checkbox" />
</div>

Initalization code belongs in the controller. ng-init is only appropriate for aliasing special properties of ng-repeat.

Upvotes: 1

Liad Livnat
Liad Livnat

Reputation: 7475

check this out: use ng-init

<div ng-init="options={};options.selected=false">
    <input type="checkbox" 
        ng-checked="options.selected"
        ng-model="options.selected"/>
</div>

Upvotes: 2

Related Questions