如何从angularjs中的运行块调用控制器函数

时间:2016-01-22 12:35:58

标签: angularjs

我是angularjs的新手。我想从运行块调用控制器函数。

有些事情: -

//index.html
  .
  .
  <li class="nav-sap"><a ng-click="loadSfData(5)" href="">Sf</a></li>
  .
  .


//app.js   

myApp.run(['$rootScope', function ($rootScope) {

   $rootScope.callFunctionFromGlobally=function(sid){

     //Here, i want call controller function
    }
}]);

1 个答案:

答案 0 :(得分:2)

您的HTML

<div id="yourcontainer" ng-app="" ng-controller="mainController">
</div>

run内,您可以调用controller功能,如下所示

var scope = angular.element(document.getElementById('yourcontainer')).scope();
scope.yourControllerMethod();