简体   繁体   English

从浏览器调用Angular Controller函数

[英]Call Angular Controller Function from Browser

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. 不使用$scope语法但使用controller as语法时,如何从浏览器控制台调用控制器的函数。 There is no ng-controller injected so I cannot use the method proposed at this SO question . 没有注入ng-controller所以我不能使用在这个SO问题上提出的方法。 I use the $stateProvider of ui-router to set my controller to a view like so: 我使用ui-router$stateProvider将控制器设置为如下所示的视图:

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

By Browser I am assuming you mean your HTML code. 通过浏览器,我假设您的意思是您的HTML代码。 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 : 然后,您可以从html进行操作:

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

Update : By browser's JS if you mean as in like within <script> tag then do, 更新 :使用浏览器的JS,如果您的意思是<script>标记中的,那么请这样做,

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. 尽管我不建议将控制器功能暴露在angular app模块之外。 But, if you want temporary fix go ahead and use this approach. 但是,如果您要临时修复,请继续使用此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM