Molly Harper Molly Harper - 1 year ago 75
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!

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download