如何在Angularjs中调用函数?

时间:2017-02-02 06:12:06

标签: php angularjs function

  <script>
  var app = angular.module('myApp', []);
  app.controller('myCtrl', function($scope, $http) 
  {
    $http.get("http://www.adhr.adnacgroup.com/ADHRM/companyJson.php")
    .then(function(response) 
    {$scope.names = $scope.names = response.data.service;});    
    function getInfo(){
      $http.post('getTask1.php').success(function(data){
         $scope.details = data;
      })
    }      
  });
</script>

<script>
  var app = angular.module('myApp', []);
  app.controller('myCtrl', function($scope, $http) 
  {
    $http.get("http://www.adhr.adnacgroup.com/ADHRM/companyJson.php")
    .then(function(response) 
    {$scope.names = $scope.names = response.data.service;});    
    function getInfo(){
      $http.post('getTask2.php').success(function(data){
         $scope.details = data;
      })
    }      
  });
</script>

<script>
  var app = angular.module('myApp', []);
  app.controller('myCtrl', function($scope, $http) 
  {
    $http.get("http://www.adhr.adnacgroup.com/ADHRM/companyJson.php")
    .then(function(response) 
    {$scope.names = $scope.names = response.data.service;});    
    function getInfo(){
      $http.post('getTask3.php').success(function(data){
         $scope.details = data;
      })
    }      
  });
</script>

我在3个不同页面上有3个脚本,使用相同的ng-app和ng-controller。

下面的代码是我的script1的gettask1.php和我为所有脚本创建的类似页面,用于从不同的表中获取记录。但问题是我只能在所有标签中只检索最后的脚本数据。我在angularjs中的新功能请提前帮助我。

<?php
ob_start();
session_start();
include"includes/dbconfig.php";
$org_id=$_SESSION['org_id'];
if(!isset($_SESSION['org_id'])){
header("Location:index.php");
}
getInfo();
$query = "SELECT * from `offer_letter` ORDER BY `offer_id` ASC";
$result = mysql_query($query);
$arr = array();
if(mysql_num_rows($result) != 0) {
while($row = mysql_fetch_assoc($result)) {
$arr[] = $row;
}
}
echo $json_info = json_encode($arr);
?>

1 个答案:

答案 0 :(得分:0)

你可以这样做:

     (function(angular) {
        angular.module('controllerExample', [])
          .controller('myctrl', ['$scope', myctrl]);
        function myctrl($scope) {     

    $scope.example=function example(){

         //your code will be  here 
       }

    function example2(){
      //do your code. You can call this function only in this controller.
    }
  example2();

    }
})(window.angular);

您可以从模板中调用example()。