0

How can I call a controller's function from my browser's console, when I do not use the $scope syntax for my functions but the controller as syntax. There is no ng-controller injected so I cannot use the method proposed at this SO question. I use the $stateProvider of ui-router to set my controller to a view like so:

$stateProvider.state('contacts', {
  template: ...,
  controller: 'ContactsCtrl as contact'
})

1 Answer 1

0

By Browser I am assuming you mean your HTML code. How are the scope functions defined within your controller definition?

Incase you are using a virtual model as in

function ContactsCtrl(){
  var vm = this;
  vm.scopeFn = function(){
    //Do sumthin
  }
}

Then you can just do this from the html :

<button ng-click="contact.scopeFn()" />

Update : By browser's JS if you mean as in like within <script> tag then do,

function ContactsCtrl(){
  var vm = this;
  vm.scopeFn = function(){
    //Do sumthin
  }
  window.globalFn = vm.scopeFn;

}

And then call it from the browser as :

<script>
  //on some event :
  window.globalFn();
</script>

I hope this helps. Although I wouldn't advice exposing a controller function outside the angular app module. But, if you want temporary fix go ahead and use this approach.

Sign up to request clarification or add additional context in comments.

2 Comments

I mean the browser's javascript console
Look at the update to the answer. I hope this is what you want.

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.