How to use ng-app and ng-controller for many pages?

313 Views Asked by At

I'm stuck in CRUD Operations in AngularJS.

  1. index.html. on this page on body I am using ng-app and ng-controller.
  2. offer_letter_dashboard.php and
  3. termination_dashboard.php on this page I am writing script for adding and fetching data.

I am using the same script on both the pages for fetching data. Here is the 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;
        });
        getInfo();
        function getInfo(){
            $http.post('gettermination.php').success(function(data){
                $scope.details = data;
            })
        }    
    });
</script>

Right now only the first page can retrieve data. Only the first page's script is working fine. And it applies for termination's page too.

1

There are 1 best solutions below

6
On

You can have multiple instances of the same controller on different pages with ng-app and ng-controller. If you have multiple controllers in the same app, you can use ng-route or ui-router to link the pages If you want to use the same script to make requests to multiple urls you can use a function in your script passing the url

  var app = angular.module('myApp', []);
  app.controller('myCtrl', function($scope, $http) 
  {
      var getData=function(url){
         $http.get(url)
         .then(function(response){
         $scope.names = $scope.names = response.data.service;});
         getInfo();
         function getInfo(){
         $http.post('gettermination.php').success(function(data)
          {
            $scope.details = data;
          })
        }
      } 

  });