Molly Harper Molly Harper - 7 months ago 49
Javascript Question

Call function on pageload when ng-checked = true in AngularJS

I am building checkboxes dynamically using Angular. I set the ng-checked value based on a number of conditions.

The checkbox is appropriately checked on pageload. I need to call the makeAddressDefault() function (currently used in ng-click) on page load when ng-checked = true.

<input type="checkbox"
ng-click="makeAddressDefault(add, $event)"
ng-checked="addresses.length === 1 || add.IsDefault() || isSelected(">
<label ng-init="isAddressDefault(addresses, add)" for="defaultAddress{{$id}}">
{{add.FirstName}} {{add.LastName}}<br />
{{add.Address1}}<br />
{{add.City}}, {{add.State}} {{add.ZIP}}

What is the best way to handle this? Thanks!


In your controller why don't you do something like

$scope.init = function(){
  angular.forEach(addresses, function(add){
    if(addresses.length === 1 || add.IsDefault() || isSelected({


It will automatically call your init function once the controller has been initialized.