1. First, let's take a look at the files we need.
2. Let's take a look.
There are two pages: first page and second page. Click two buttons to switch between different pages and display different styles.
3. Okay. Let's take a look at the code!
<!DOCTYPE html>
Code explanation:
First, we need to introduce three files.
1) angular. min. js ---- angularJS script
2) angular-css.js ---- scripts used to convert css
3) angular-route.js ---- routing script
Then we need two anchors.
<a href="#first" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >First Page</a><a href="#second" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Second Page</a>
Finally, we need a container for Route import.
Copy codeThe Code is as follows: <div ng-view> </div>
Then, we put the routing configuration, service, and controller in the app. js, services. js, and controller. js files to facilitate code management and maintenance.
4. Next, let's take a look at the routing section.
angular.module('myApp',['ngRoute','angularCSS']) .config(['$routeProvider',function ($routeProvider) { $routeProvider .when('/first',{ templateUrl : './view/first.html', controller : 'FirstCtrl as firstCtrl' }) .when('/second',{ templateUrl : './view/second.html', controller : 'SecondCtrl as secondCtrl' }) .otherwise({ redirectTo : '/first' }) }])
Code explanation:
1) First, inject ngRoute and angularCSS dependencies into the myApp module.
2) then configure the route (config ):
The config function of the AngularJS module is used to configure routing rules. By using configAPI, we request to inject $ routeProvider into our configuration function and use $ routeProvider. whenAPI to define our routing rules.
$ RouteProvider provides the when (path, object) & otherwise (object) function to define all routes in order. The function contains two parameters:
The first parameter is URL or URL regular rules. The second parameter is the route configuration object.
3) controller
Function, string, or array type. Execute the controller function on the current template to generate a new scope.
4) controllerAs
String type. The alias is specified for the controller.
5) redirectTo
Redirected address
6) resolve
Specify other modules on which the current controller depends.
Overview of route setting objects:
5. Let's take a look at the service section, service. js.
angular.module('myApp') .factory('FirstService',[function () { var list = [ { name : 'Rose',age : 10 }, { name : 'Tom',age : 19 } ]; return { getList : function () { return list; } } }])
Note: angular. module ('myapp') does not require dependency injection.
6. Let's take a look at controller integration, controller. js.
angular.module('myApp') .controller('FirstCtrl',['$css','FirstService',function ($css,$service) { var self = this; $css.add('css/first.css'); self.list = function () { return $service.getList(); } }]) .controller('SecondCtrl',['$css','FirstService',function ($css,$service) { var self = this; $css.add('css/second.css'); self.list = function () { return $service.getList(); } }])
Code Analysis:
1) inject service dependencies into the controller and # css Dependencies
Copy codeThe Code is as follows: controller ('firstctrl ', [' $ css ', 'firstservice', function ($ css, $ service)
2) Add the css dependency path
Copy codeThe Code is as follows: $ css. add ('css/first.css ');
Note: angular. module ('myapp') does not require dependency injection.
7. Okay. The logic section has been completed. The following shows our style and structure section.
First.html
<div class='first'>
Second.html
<div class='second'>
First.css
.first{ background-color: yellow;}.first *{ color: red;}
Second.css
.second{ background-color: skyblue;}.second *{ color: green;}