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'
})
Community
  • 1
  • 1
niklas
  • 2,887
  • 3
  • 38
  • 70

1 Answers1

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Rohit Rane
  • 2,790
  • 6
  • 25
  • 41