SoFunction
Updated on 2025-04-07

Routing and page transfer examples

Page parameter transfer method: 1. $rootScope. 2. (url)/user/:name/:age.

Page conversion method: 1. href="#/" rel="external nofollow" rel="external nofollow" rel="external nofollow" . 2. $. 3. $. 4. ui-sref

(1) Bring your own route ngRoute

<html> 
  <head> 
    <meta charset="utf-8"> 
    <title>AngularJS Routing instance</title> 
  </head> 
  <body ng-app='routingDemoApp' ng-controller="myCtrl"> 
    <h2>AngularJS Routing Application</h2> 
       name: <input type="text" ng-model="names"><br> 
    <ul> 
      &lt;li&gt;&lt;a href="#/" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Home 1</a></li>      &lt;li&gt;&lt;a href="#/second/2/3" rel="external nofollow" &gt;second&lt;/a&gt;&lt;/li&gt; 
      &lt;li&gt;&lt;a href="#/printers" rel="external nofollow" >Print</a></li>      &lt;li&gt;&lt;a href="#/blabla" rel="external nofollow" >Other</a></li>    &lt;/ul&gt; 
    &lt;div ng-view&gt;&lt;/div&gt; 
    &lt;script src="/libs/jquery/1.10.2/" &gt;&lt;/script&gt; 
    &lt;script src="/libs//1.4.6/"&gt;&lt;/script&gt; 
    &lt;script src="/libs/angular-route/1.3.13/"&gt;&lt;/script&gt; 
    &lt;script&gt; 
    var transform =function(data){return $.param(data);  }  
      var app=('routingDemoApp',['ngRoute']); 
      ('myCtrl', function($scope,$http, $rootScope) { 
         $http({ 
          method:'POST', 
          url:"http://localhost:8090/angu_demo/", 
          data:{"age":20 } 
         })  
        .success(function(data,header,config,status){ 
        //Response successfully          $ = data[0].age; 
          $="rrrrrr"; 
 
        }).error(function(data,header,config,status){ 
        //Processing response failed        }); 
      }); 
      ('AboutController', function($scope,$http,$rootScope,$routeParams) { 
         
         $ = $; 
        $ = $;  
        $=$; 
       
      }) 
      (['$routeProvider', function($routeProvider){ 
        $routeProvider 
        .when('/',{template:'This is the home page'}) 
        .when('/second/:id/:age', 
          {templateUrl: '', 
          controller: 'AboutController'} 
        ) 
        .when('/printers',{template:'This is the printer page'}) 
        .when('/second_2',{template:'This is second_2'}) 
        .otherwise({redirectTo:'/'}); 
      }]); 
       
      
    &lt;/script&gt; 
    
    
  &lt;/body&gt; 
&lt;/html&gt; 

(2)ui-router

&lt;html&gt; 
  &lt;head&gt; 
    &lt;meta charset="utf-8"&gt; 
    &lt;title&gt;AngularJS Routing instance &lt;/title&gt; 
     &lt;script src="/libs//1.4.6/"&gt;&lt;/script&gt;  
 
    &lt;script src="/angular-ui-router/1.0.0-beta.3/"&gt;&lt;/script&gt;  
   
  &lt;/head&gt; 
  &lt;body ng-app="routerApp" &gt; 
  &lt;div ng-controller="MainCtrl"&gt; 
    &lt;ul&gt; 
      &lt;li&gt;&lt;a href="#/" rel="external nofollow" rel="external nofollow" rel="external nofollow" >Home 1</a></li>      &lt;li&gt;&lt;a href="#/second/" rel="external nofollow" &gt;second&lt;/a&gt;&lt;/li&gt; 
      &lt;li&gt;&lt;a href="#/third" rel="external nofollow" &gt;third&lt;/a&gt;&lt;/li&gt; 
    &lt;/ul&gt; 
    &lt;a href="#/fourth/42" rel="external nofollow" >href pass parameters</a>    &lt;a ui-sref="fifth({'name':123,'id':256})"&gt;ui-srefPass parameters&lt;/a&gt; 
    &lt;button ng-click="ngclick_go()" class="btn btn-primary " &gt;Pass parameters&lt;/button&gt; 
     &lt;button ng-click="ngclick_location()" class="btn btn-primary " &gt;locationPass parameters&lt;/button&gt; 
     &lt;div ui-view&gt;&lt;/div&gt;  
     &lt;div ui-view="second0"&gt;&lt;/div&gt;  
    &lt;div ui-view="second1"&gt;&lt;/div&gt;  
    &lt;div ui-view="second2"&gt;&lt;/div&gt;  
&lt;/div&gt; 
  &lt;script type="text/javascript"&gt; 
   /* var app = ('routerApp', ['']); */ 
   var app=('routerApp',['']); 
   ('MainCtrl', function($scope, $state,$location) { 
     $scope.ngclick_go = function() { 
       $('sixth',{name: 42}); // The URL after jump: #/camnpr/appManager      }; 
      $scope.ngclick_location = function() { 
        $('/sixth/detail/42'); // The function is also jumped      }; 
      
      
     
    }); 
    (function($stateProvider, $urlRouterProvider) { 
      $('/second');  //The difference from the native $routerProvider is that $urlRouterProvider first writes the default path      $stateProvider   //Replace the $() method with $('',{}).state('',{})..        .state('second', { 
          url: '/second', 
           views: {'second0': { 
              templateUrl: '' ,  //See templateUrl: There are many templates included in the back              controller: 'MainCtrl'  
            }, 
            'second1': { 
              templateUrl: '', 
              controller: 'MainCtrl' 
               
            }, 
            'second2': { 
              templateUrl: '', 
              controller: 'MainCtrl' 
            } 
          }  
        }) 
        .state('third', { 
          url: '/third', 
          templateUrl: '' ,   //See templateUrl: There are many templates included in the back          controller: 'MainCtrl'  
        }) 
         
         .state('fourth', { 
          url: '/fourth/:name', 
          templateUrl: '' ,    //See templateUrl: There are many templates included in the back          controller: function ($stateParams,$scope) { 
            $=$; 
            alert(=$) 
          } 
 
        }) 
         .state('fifth', { 
          url: '/fifth/:name/:id', 
          templateUrl: '' ,    //See templateUrl: There are many templates included in the back          controller: function ($stateParams,$scope) { 
            alert($+"  "+$) 
          } 
 
        }) 
        .state('sixth', { 
          url: '/sixth/detail/:name', 
          templateUrl: '' ,    //See templateUrl: There are many templates included in the back          controller: function ($stateParams,$scope) { 
            alert($) 
          } 
 
        }) 
        /* .state('fourth', {
           url: '/fourth/:name',
           templateUrl: '' , //See templateUrl: There are many templates behind it
           controller: function ($stateParams,$scope) {
             $=$;
           }
 
         }) */ 
         
    }); 
  
   &lt;/script&gt; 
    
  &lt;/body&gt; 
&lt;/html&gt; 

Download address:angu_demo_jb51.rar

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.