Using Angularjs to build a single page application, the method of loading JS file according to demand

Source: Internet
Author: User

We can change the view in the Angularjs according to the URL, dynamically load the page template, but the control template controller needs to be defined before you can, and the page template at the same time to load, so that the entire site of JS must be loaded first.

       requirejs is the first thing we think of, but the volume is a bit big, on GitHub on the individual with it wrote the angular dynamic load controller module, is through JS Promise asynchronous programming technology, The Final solution is simple ~ ~
First add resolve property in $routeprovider,

$routeProvider.        When ('/phones ', {               templateurl: ' partials/phone-list.html ',                Controller:phonelistctrl,                resolve: Phonelistctrl.resolve})


And then
function Phonelistctrl ($scope) {  ///itself without tube, how to get}phonelistctrl.resolve = {  delay:function ($q) {    var Delay = $q. Defer (),    load = function () {        $.getscript ('/js/xxxxx.js ', function () {        delay.resolve ();        });    };    load ();    return delay.promise;}  }

With this technology, our Hybridapp can be divided into static packaging in the native application of the part, and placed on the server dynamic part, easy to upgrade the main app without the premise of adding some temporary page function. Specific application scenarios Let's think for yourself!

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: info-contact@alibabacloud.com 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.