AngularJS implements routing instances and angularjs routing instances

Source: Internet
Author: User

AngularJS implements routing instances and angularjs routing instances

1. First we will introduce angular. js and angular-route.js files

Copy codeThe Code is as follows: <script type = "text/javascript" src = lib/angular. min. js> </script> <script type = "text/javascript" src = lib/angular-route.js> </script>

2. Create an anchor and container (ng-view) in html)

<A href = "# first" rel = "external nofollow" rel = "external nofollow"> page 1 </a> <a href = "# second" rel = "external nofollow"> page 2 </a> <div ng-view> </div>

3. Inject ngRoute dependency into the module

Copy codeThe Code is as follows: angular. module ('myapp', ['ngroup'])

4. Configure routes

config(['$routeProvider',function ($routeProvider) {  $routeProvider.when('/first',{    template : '

Effect display:


Complete code:

<! DOCTYPE html> 

Next we will create a simulated project route

1. First, let's take a look at the files we need.


Show all files

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!

Index.html

<!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;}

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: 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.