[AngularJS] using ANGULARCSS to dynamically load CSS preamble
The two articles above show how to use ANGULARAMD to dynamically load controllers and service. This article, based on this, describes how to use ANGULARCSS to dynamically load CSS, making project functionality more modular, and increasing the productivity of development and maintenance. Keep a record for yourself, and hope to help developers in need.
Installation
This article is based on an example program that uses ANGULARAMD to dynamically load service", adding the ability to dynamically load CSS. Before entering the development steps of this article, the developer can follow the steps of the previous article to establish the infrastructure.
Dynamic Load Service Example: Click here to download
After getting the infrastructure, open the command prompt character CD to the infrastructure folder by entering the following instructions, you can use Bower to get the Angularcss suite.
bower install angular-css
After completing the above steps, open the Bower_components folder inside the working folder, you can find added angular-css this package.
Mount Angularcss
After the ANGULARCSS Kit is acquired, the kit is then mounted to the angular of the system. First, edit the existing App.js file in the working folder, modify the Require.js setting parameters in the file, and use it to add the package path and dependency of ANGULARCSS when the system is running. (for the use of related Require.js, refer to: require.js usage-Nanyi's blog)
require.config({ paths: { // ...... // angularCSS "angularCSS": "bower_components/angular-css/angular-css" }, shim: { // ...... // angularCSS "angularCSS": ["angular"] }});
After modifying the Require.js setting parameters, in the same app.js, modify the following require syntax to add angularcss to the list of loading kits for the project. (The use of the relevant require syntax, also refer to: require.js usage-Nanyi blog)
// bootstrapdefine(["angular", "angularAMD", "angular-ui-router", "angularCSS"], function (angular, angularAMD) { // ......});
Then, within the same require syntax, modify the following angular syntax to start the Angularcss suite. Here to pay special attention, ANGULARCSS module name is "Door3.css", be careful not to hit the wrong. (Introduction to the use of related angular, recommendations for reference: AngularJS Build and Execute-Shyam Seshadri, Brad Green)
// modulevar app = angular.module("app", ["ui.router", "door3.css"]);
Finally, within the require syntax, the CSS parameters provided by the ANGULARCSS syntax are used to define the CSS files that each ui-router route will dynamically load when it is started. (for the CSS parameter definitions provided by the relevant ANGULARCSS, refer to: Angularamd:for States (UI Router)-Door3)
// route$stateProvider // home .state("home", angularAMD.route({ url: "/home", templateUrl: "home.html", controllerUrl: "home.js", css: "home.css" })) // home .state("about", angularAMD.route({ url: "/about", templateUrl: "about.html", controllerUrl: "about.js", css: "about.css" }));
Develop template, CSS
After modifying app.js to Mount Angularcss, you can start using CSS syntax to build the CSS file used by each route in the system.
-
Home.css
h1 { color: blue; }
-
About.css
h1 { color: red; }
Perform
After you complete the development steps, you are ready to use Chrome to perform index.html to examine the results. But before looking at the results, it's important to use the following data to turn on the necessary features of Chrome and follow through with chrome to perform index.html normally.
- Local pages in Chrome, using XMLHttpRequest to read local archives-sleeping areas
After executing the index.html, the system will enter the preset home page according to the routing settings. Using Chrome's developer tools, you can see that the system loads the template, Controller, CSS, and display on the home page.
Click the About button on the home page to switch to the About page. Also from the Chrome Developer tool, you can see that the system is clicked on the About button, only to load the About page template, Controller, Service, CSS to display on the page, This is also the dynamic load CSS functionality provided by ANGULARCSS.
Sample Download
Sample Archives: Click here to download
[AngularJS] Dynamically loading CSS using ANGULARCSS