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">
1
  • 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: stackoverflow.com/questions/17547917 Commented Aug 18, 2016 at 10:16

1 Answer 1

0

add this code in your controller

if(!angular.equals(vm.value, null)){
   vm.execute(vm.value)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.