简体   繁体   中英

Angular loading Controllers and Services

I am using Angularjs for my application.I am having one common page which has header and footer which i am making common for all pages.Thats yi am placing it in one common html.Only contents code i am placing in other html pages.

As i am using one common page i am loading all controllers and all Services that i am using in the application.

Here is my commonpage.html

    <!DOCTYPE html>
    <html lang="en" data-ng-app="adminApp">
    <head>
    </head>
    <body>
    <!--Here is header code-->
    <div class="LeftMenu">
    <ul class="navbar">
    <a href="#!/admindashboardhome" title="Dashboard"><li>
    <span>Dashboard</span></li>
    </a> 
    <a href="#!/examinationhalltickets" title="Declaration"><li>
    <span>Examination Form</span></li>
    </a>
    <a href="#!/collegedetails" title="Declaration"><li>College 
    Details</li>
    </a>
    </ul>
    </div>
    <!--followed by footer code-->
    <div data-ng-view>  <!--ng-view-->
    </div>
    <!--Here i am loading all controllers and services related to application-->
    <script
    src="resources/angular/controller/admin/AdminExamController.js">
    </script>
    <script src="resources/angular/service/admin/AdminExamService.js">
    </script>
    <!-- And many more in same fashion-->
    </body>
    </html>

The doubt i am having is,is it necessary to place all controllers and services like i am doing because i am facing performance issue even though i am connected to strong internet it loads very slow.As i am placing all in one page it is loading all controllers and services everytime.If i place controllers in their respective html then i am getting error like ExamController.js or any .js Controller not defined.Is there any other way that i can load all controllers and services so that i can increase the performance of the application?

I think this is what your looking for

app.js

    /* Module Creation */
var app = angular.module ('adminApp', ['ngRoute']);

app.config(['$routeProvider', '$controllerProvider', function($routeProvider, $controllerProvider){

/*Creating a more synthesized form of service of $ controllerProvider.register*/
app.registerCtrl = $controllerProvider.register;

function loadScript(path) {
  var result = $.Deferred(),
  script = document.createElement("script");
  script.async = "async";
  script.type = "text/javascript";
  script.src = path;
  script.onload = script.onreadystatechange = function (_, isAbort) {
      if (!script.readyState || /loaded|complete/.test(script.readyState)) {
         if (isAbort)
             result.reject();
         else
            result.resolve();
    }
  };
  script.onerror = function () { result.reject(); };
  document.querySelector("head").appendChild(script);
  return result.promise();
}

function loader(arrayName){

    return {
      load: function($q){
                var deferred = $q.defer(),
                map = arrayName.map(function(name) {
                    return loadScript(name+".js");
                });

                $q.all(map).then(function(r){
                    deferred.resolve();
                });

                return deferred.promise;
        }
    };
}

$routeProvider  
    .when('/view2', {
        templateUrl: 'view2.html',
        resolve: loader(['Controller2'])
    })
    .when('/bar',{
        templateUrl: 'view1.html',
        resolve: loader(['Controller1'])
    })
    .otherwise({
        redirectTo: document.location.pathname
    });
}]);

index.html

<!DOCTYPE html>
    <html lang="en">
    <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.min.js"></script>      
    </head>
    <body ng-app="adminApp">
    <!--Here is header code-->
    <div class="LeftMenu">
    <ul class="navbar">
    <a href="#!/admindashboardhome" title="Dashboard"><li>
    <span>Dashboard</span></li>
    </a> 
    <a href="#!/examinationhalltickets" title="Declaration"><li>
    <span>Examination Form</span></li>
    </a>
    <a href="#!/collegedetails" title="Declaration"><li>College 
    Details</li>
    </a>
    </ul>
    </div>
    <!--followed by footer code-->
    <div data-ng-view>  <!--ng-view-->
    </div>
    <!--Here i am loading all controllers and services related to application-->
    <script
    src="app.js">

    </script>
    <!-- And many more in same fashion-->
    </body>
    </html>

Controller 1.js

(function(val){
    'use strict';

    angular.module('Controller1App', [])
    .controller('Controller1', ['$http','$rootScope','$scope','$window', function($http,$rootScope, $scope, $window){       

        //Your code goes here

    }])


})(this);

Controller 2.js

(function(val){
    'use strict';

    angular.module('Controller2App', [])
    .controller('Controller2', ['$http','$rootScope','$scope','$window', function($http,$rootScope, $scope, $window){       

        //Your code goes here

    }])


})(this);

Refer https://plnkr.co/edit/cgkgG5PCwJBVOhQ1KDW2?p=preview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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