Detailed description of angular-based requireJs loading js and angularrequirejs on demand

Source: Internet
Author: User

Detailed description of angular-based requireJs loading js and angularrequirejs on demand

I am not busy recently !! You have time to accumulate the angular stuff you learned !!

Angular routes must be familiar to everyone! (For strangers, refer to my previous article and teach you how to configure angular routes !)

Angular routing is used as a single page application. When switching a page, it is a page. Therefore, switching between controller and Loading Control js becomes a big problem !! After a long time, the angular-route built-in solution did not solve this problem. In the end, I used requireJs to solve this problem !! Code on!

1. First introduce requireJs, and write and configure requirejs (['framework']) with a closure under it. This means that the first time you enter the page, load the framework

<script src="js/lib/require.min.js"></script> <script>   (function () {     var jsDir = '/js/';     var jsLibDir = '/js/lib/';     var jsComponentDir = '/components/';     var paths = {       angular: jsLibDir + 'angular.min',       angularRoute: jsLibDir + 'angular-route.min',       jquery: jsLibDir + 'jquery.min',       jQueryMD5: jsLibDir + 'jquery.md5',       highcharts: jsLibDir + 'highcharts',       radialProgress: jsLibDir + 'radialProgress',       d3: jsLibDir + 'd3.min',       echarts: jsLibDir + 'echarts',       framework: jsDir + 'framework',       angularUtil: jsDir + 'angular-util',       standardDashboard: jsDir + 'standard-dashboard',       standardConsole: jsDir + 'standard-console',       standardAmountStatistic: jsDir + 'standard-amount-statistic',       standardReport: jsDir + 'standard-report',       standardAdvancedReport: jsDir + 'standard-advanced-report',       standardExpertAnswer: jsDir + 'standard-expert-answer',       standardService: jsDir + 'standard-service',       standardStrategyInform: jsDir + 'standard-strategy-inform',       standardMember: jsDir + 'standard-member',       standardSchedule: jsDir + 'standard-schedule',       standardChannel: jsDir + 'standard-channel',       standardStrategyMerge: jsDir + 'standard-strategy-merge',       standardIntegrate: jsDir + 'standard-integrate',       standardPersonalCenter: jsDir + 'standard-personal-center',       dateTimePicker: jsComponentDir + 'dateTimePicker/date-time-picker',       fullCalendar: jsComponentDir + 'fullCalendar/fullcalendar',       moment: jsComponentDir + 'fullCalendar/moment'     };          requirejs.config({       paths: paths,       shim: {         angular: {           exports : 'angular',           deps: ['jquery']         },         angularRoute: {           deps: ['angular']         },         jQueryMD5: {           deps: ['jquery']         }       },       //urlArgs: "timeStamp=" + (new Date()).getTime()       //urlArgs: 'v=1.47.1&t=20160719'     });     requirejs(['framework']);   }()); </script> 

2. As the first js load, framework. js plays a crucial role !! Define the frameworkApp module as the main module, load the Public Service utilmodel and ngRoute route, and define a resolveController method. The callback function is requireJs, which will be discussed later!

// Introduce the module var frameworkApp = angular. module ('frameworkapp', ['ngroup', 'utilmodule']); // load the corresponding controller var resolveController = function (names, dependences ES) {// console. log (names) // console. log (dependances) return {loadController: ['$ Q',' $ rootScope ', function ($ q, $ rootScope) {var defer = $ q. defer (); require (names, function () {defer. resolve (); $ rootScope. $ apply () ;}); return defer. promise ;}] };};

3. Configure the route and use the resolve Method to complete the callback. Note that the callback is a list and the value is the module name defined in step 1.

frameworkApp.config(['$routeProvider', '$controllerProvider', '$provide', '$compileProvider', '$filterProvider',   function ($routeProvider, $controllerProvider, $provide, $compileProvider, $filterProvider) {     frameworkApp.register = {       controller: $controllerProvider.register,       factory: $provide.factory,       service: $provide.service,       filter: $filterProvider.register,       directive: $compileProvider.directive     };     $routeProvider       .when('/',{         redirectTo: '/dashboard'       })       .when('/dashboard',{         templateUrl: 'dashboard.html',         controller: 'DashboardCtrl',         resolve: resolveController(['standardDashboard', 'd3','radialProgress','highcharts'])       })       .when('/console',{         templateUrl: 'console.html',         controller: 'ConsoleCtrl',         resolve: resolveController(['standardConsole'])       })       .when('/amountStatistic',{         templateUrl: 'amount-statistic.html',         controller: 'amountStatisticCtrl',         resolve: resolveController(['standardAmountStatistic','highcharts','dateTimePicker'])       })       .when('/report',{         templateUrl: 'report.html',         controller: 'ReportCtrl',         resolve: resolveController(['standardReport','dateTimePicker'])       })       .when('/advancedReport',{         templateUrl: 'advanced-report.html',         controller: 'advancedReportCtrl',         resolve: resolveController(['standardAdvancedReport','highcharts','dateTimePicker'])       })       .when('/expertAnswer',{         templateUrl: 'expert-answer.html',         controller: 'expertAnswerCtrl',         resolve: resolveController(['standardExpertAnswer'])       })       .when('/service',{         templateUrl: 'service.html',         controller: 'ServiceCtrl',         resolve: resolveController(['standardService'])       })       .when('/strategy-inform',{         templateUrl: 'strategy-inform.html',         controller: 'StrategyInformCtrl',         resolve: resolveController(['standardStrategyInform'])       })       .when('/member',{         templateUrl: 'member.html',         controller: 'MemberCtrl',         resolve: resolveController(['standardMember'])       })       .when('/schedule',{         templateUrl: 'schedule.html',         controller: 'ScheduleCtrl',         resolve: resolveController(['standardSchedule'])       })       .when('/channel',{         templateUrl: 'channel.html',         controller: 'ChannelCtrl',         resolve: resolveController(['standardChannel'])       })       .when('/strategy-merge',{         templateUrl: 'strategy-merge.html',         controller: 'StrategyMergeCtrl',         resolve: resolveController(['standardStrategyMerge'])       })       .when('/integrate',{         templateUrl: 'integrate.html',         controller: 'IntegrateCtrl',         resolve: resolveController(['standardIntegrate'])       })       .when('/personalCenter',{         templateUrl: 'personal-center.html',         controller: 'PersonalCenterCtrl',         resolve: resolveController(['standardPersonalCenter'])       })       .otherwise({         redirectTo: '/error'       });    }]); 

4. You are done ~ Switch controller and load js as needed !!! La la!

5. I did not solve this problem. When I introduced echarts, I reported an error using this method. I changed it to highcharts. I didn't bring echarts in for a long time, but I found it, you can introduce echarts before require takes effect! Please explain it !!

<!-- start build -->   <script src="js/lib/echarts.js"></script>   <script src="js/lib/require.min.js"></script> 

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.