Run ng change functions on boot for pre-set checkboxes

I have a function in ng-change that executes every time the checkbox is checked (manually).

Now I want the checkboxes to be pre-checked at boot time, and the function inside ng-change also executed (at boot).

What are some of the approaches I can take to achieve this?

+4
source share
3 answers

You do not need to do this, your model will automatically do this, say

$ scope.customer = client;

<input type='checkbox' ng-model='customer.active' /> Is Active

It will automatically take care

0
source

, onChange, ng-init

onLoad onChange, :

$scope.onLoad = function(params) {
  // do some init functionality and call onChange functionality
  onChange();
}

$scope.onChange = function(value) {
  // do function when a combobox is chaned
}

HTML-

<div ng-init="onLoad()">
  <input type='checkbox' ng-model='someModel' ng-change="('1')"/> Is one
  <input type='checkbox' ng-model='someModel' ng-change="('2')"/> Is two
  <input type='checkbox' ng-model='someModel' ng-change="('3')"/> Is three
</div>
0

ng-change , , angular docs . , ng-change, , . , .

$scope.$watch("myModel", function(newValue, oldValue){
    // do something
});
0

All Articles