angular. js routing and page parametric example

  • 2021-07-24 09:42:53
  • OfStack

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

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

(1) Self-contained routing 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> 
      <li><a href="#/" rel="external nofollow" rel="external nofollow" rel="external nofollow" > Home page 1</a></li> 
      <li><a href="#/second/2/3" rel="external nofollow" >second</a></li> 
      <li><a href="#/printers" rel="external nofollow" > Printer </a></li> 
      <li><a href="#/blabla" rel="external nofollow" > Others </a></li> 
    </ul> 
    <div ng-view></div> 
    <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js" ></script> 
    <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script> 
    <script src="http://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.js"></script> 
    <script> 
    var transform =function(data){return $.param(data);  }  
      var app=angular.module('routingDemoApp',['ngRoute']); 
      app.controller('myCtrl', function($scope,$http, $rootScope) { 
         $http({ 
          method:'POST', 
          url:"http://localhost:8090/angu_demo/test.chtm", 
          data:{"age":20 } 
         })  
        .success(function(data,header,config,status){ 
        // Successful response  
          $scope.names = data[0].age; 
          $rootScope.name="rrrrrr"; 
 
        }).error(function(data,header,config,status){ 
        // Failed to process response  
        }); 
      }); 
      app.controller('AboutController', function($scope,$http,$rootScope,$routeParams) { 
         
         $scope.id = $routeParams.id; 
        $scope.age = $routeParams.age;  
        $scope.name=$rootScope.name; 
       
      }) 
      app.config(['$routeProvider', function($routeProvider){ 
        $routeProvider 
        .when('/',{template:' This is the home page '}) 
        .when('/second/:id/:age', 
          {templateUrl: 'second.html', 
          controller: 'AboutController'} 
        ) 
        .when('/printers',{template:' This is the printer page '}) 
        .when('/second_2',{template:' This is second_2'}) 
        .otherwise({redirectTo:'/'}); 
      }]); 
       
      
    </script> 
    
    
  </body> 
</html> 

(2)ui-router


<html> 
  <head> 
    <meta charset="utf-8"> 
    <title>AngularJS  Routing instance  </title> 
     <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>  
 
    <script src="http://cdn.bootcss.com/angular-ui-router/1.0.0-beta.3/angular-ui-router.js"></script>  
   
  </head> 
  <body ng-app="routerApp" > 
  <div ng-controller="MainCtrl"> 
    <ul> 
      <li><a href="#/" rel="external nofollow" rel="external nofollow" rel="external nofollow" > Home page 1</a></li> 
      <li><a href="#/second/" rel="external nofollow" >second</a></li> 
      <li><a href="#/third" rel="external nofollow" >third</a></li> 
    </ul> 
    <a href="#/fourth/42" rel="external nofollow" >href Transmission parameter </a> 
    <a ui-sref="fifth({'name':123,'id':256})">ui-sref Transmission parameter </a> 
    <button ng-click="ngclick_go()" class="btn btn-primary " >state.go Transmission parameter </button> 
     <button ng-click="ngclick_location()" class="btn btn-primary " >location Transmission parameter </button> 
     <div ui-view></div>  
     <div ui-view="second0"></div>  
    <div ui-view="second1"></div>  
    <div ui-view="second2"></div>  
</div> 
  <script type="text/javascript"> 
   /* var app = angular.module('routerApp', ['ui.router']); */ 
   var app=angular.module('routerApp',['ui.router']); 
   app.controller('MainCtrl', function($scope, $state,$location) { 
     $scope.ngclick_go = function() { 
       $state.go('sixth',{name: 42}); //  After jumping URL :  #/camnpr/appManager 
      }; 
      $scope.ngclick_location = function() { 
        $location.path('/sixth/detail/42'); //  The function is also jumping  
      }; 
      
      
     
    }); 
    app.config(function($stateProvider, $urlRouterProvider) { 
      $urlRouterProvider.otherwise('/second');  // And native $routerProvider No writing 1 It's like this $urlRouterProvider Write the default path first  
      $stateProvider   // Reuse $stateProvider.state('',{}).state('',{})... Substitute $routerProvider.when() Method  
        .state('second', { 
          url: '/second', 
           views: {'second0': { 
              templateUrl: 'second0.html' ,  // See templateUrl: There are many templates in the back  
              controller: 'MainCtrl'  
            }, 
            'second1': { 
              templateUrl: 'second1.html', 
              controller: 'MainCtrl' 
               
            }, 
            'second2': { 
              templateUrl: 'second2.html', 
              controller: 'MainCtrl' 
            } 
          }  
        }) 
        .state('third', { 
          url: '/third', 
          templateUrl: 'third.html' ,   // See templateUrl: There are many templates in the back  
          controller: 'MainCtrl'  
        }) 
         
         .state('fourth', { 
          url: '/fourth/:name', 
          templateUrl: 'forth.html' ,    // See templateUrl: There are many templates in the back  
          controller: function ($stateParams,$scope) { 
            $scope.name=$stateParams.name; 
            alert(=$stateParams.name) 
          } 
 
        }) 
         .state('fifth', { 
          url: '/fifth/:name/:id', 
          templateUrl: 'fifth.html' ,    // See templateUrl: There are many templates in the back  
          controller: function ($stateParams,$scope) { 
            alert($stateParams.name+"  "+$stateParams.id) 
          } 
 
        }) 
        .state('sixth', { 
          url: '/sixth/detail/:name', 
          templateUrl: 'sixth.html' ,    // See templateUrl: There are many templates in the back  
          controller: function ($stateParams,$scope) { 
            alert($stateParams.name) 
          } 
 
        }) 
        /* .state('fourth', { 
          url: '/fourth/:name', 
          templateUrl: 'third1.html' ,    // See templateUrl: There are many templates in the back  
          controller: function ($stateParams,$scope) { 
            $scope.name=$stateParams.name; 
          } 
 
        }) */ 
         
    }); 
  
   </script> 
    
  </body> 
</html> 

Download address: angu_demo_jb51.rar


Related articles: