0

With angularjs I have the following execute() function,it print the value of checked radio button and it works fine,but I need to execute this function when page loaded just if one radio button has checked.

-in this case when page loaded it should print 3 in console

-if vm.value=null function shouldn't execute

I need to trigger function only if one radio button has checked

vm.value="3";

vm.execute=function(value){
  console.log(value);
}

Html code

<input type="radio" ng-change=vm.execute(1) value="1" ng-model="vm.value">
<input type="radio" ng-change=vm.execute(2) value="2" ng-model="vm.value">
<input type="radio" ng-change=vm.execute(3) value="3" ng-model="vm.value">
<input type="radio" ng-change=vm.execute(4) value="4" ng-model="vm.value">
Sabith
  • 1,628
  • 2
  • 20
  • 38
Ali-Alrabi
  • 1,515
  • 6
  • 27
  • 60
  • I don't fully understand your question, so I'll offer two ways: You may execute function in controller by yourself by just calling it: `vm.execute(vm.value)`, so when the controller is loaded, it would be executed. Or you may create a directive: http://stackoverflow.com/questions/17547917/ – notgiorgi Aug 18 '16 at 10:16

1 Answers1

0

add this code in your controller

if(!angular.equals(vm.value, null)){
   vm.execute(vm.value)
}
byteC0de
  • 5,153
  • 5
  • 33
  • 66