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,967
  • 3
  • 35
  • 65

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
  • 44,061
  • 15
  • 107
  • 146
Rohit Rane
  • 2,592
  • 5
  • 20
  • 40