angular.js 路由及页面传参示例
程序员文章站
2022-07-05 20:16:31
页面传参数方法:1、$rootscope。2、(url)/user/:name/:age。
页面转换方法:1、href="#/" rel="external nofo...
页面传参数方法:1、$rootscope。2、(url)/user/:name/:age。
页面转换方法:1、href="#/" rel="external nofollow" rel="external nofollow" rel="external nofollow" 。2、$state.go。3、$location.path。4、ui-sref
(1)自带路由ngroute
<html> <head> <meta charset="utf-8"> <title>angularjs 路由实例</title> </head> <body ng-app='routingdemoapp' ng-controller="myctrl"> <h2>angularjs 路由应用</h2> 名: <input type="text" ng-model="names"><br> <ul> <li><a href="#/" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页1</a></li> <li><a href="#/second/2/3" rel="external nofollow" >second</a></li> <li><a href="#/printers" rel="external nofollow" >打印机</a></li> <li><a href="#/blabla" rel="external nofollow" >其他</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){ //响应成功 $scope.names = data[0].age; $rootscope.name="rrrrrr"; }).error(function(data,header,config,status){ //处理响应失败 }); }); 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:'这是首页页面'}) .when('/second/:id/:age', {templateurl: 'second.html', controller: 'aboutcontroller'} ) .when('/printers',{template:'这是打印机页面'}) .when('/second_2',{template:'这是second_2'}) .otherwise({redirectto:'/'}); }]); </script> </body> </html>
(2)ui-router
<html> <head> <meta charset="utf-8"> <title>angularjs 路由实例 </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" >首页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传参数</a> <a ui-sref="fifth({'name':123,'id':256})">ui-sref传参数</a> <button ng-click="ngclick_go()" class="btn btn-primary " >state.go传参数</button> <button ng-click="ngclick_location()" class="btn btn-primary " >location传参数</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}); // 跳转后的url: #/camnpr/appmanager }; $scope.ngclick_location = function() { $location.path('/sixth/detail/42'); // 功能也是跳转的 }; }); app.config(function($stateprovider, $urlrouterprovider) { $urlrouterprovider.otherwise('/second'); //与原生的$routerprovider写法不一样的就是$urlrouterprovider先写默认路径 $stateprovider //再用$stateprovider.state('',{}).state('',{})...代替$routerprovider.when()方法 .state('second', { url: '/second', views: {'second0': { templateurl: 'second0.html' , //看到templateurl:后面包含了很多的模板 controller: 'mainctrl' }, 'second1': { templateurl: 'second1.html', controller: 'mainctrl' }, 'second2': { templateurl: 'second2.html', controller: 'mainctrl' } } }) .state('third', { url: '/third', templateurl: 'third.html' , //看到templateurl:后面包含了很多的模板 controller: 'mainctrl' }) .state('fourth', { url: '/fourth/:name', templateurl: 'forth.html' , //看到templateurl:后面包含了很多的模板 controller: function ($stateparams,$scope) { $scope.name=$stateparams.name; alert(=$stateparams.name) } }) .state('fifth', { url: '/fifth/:name/:id', templateurl: 'fifth.html' , //看到templateurl:后面包含了很多的模板 controller: function ($stateparams,$scope) { alert($stateparams.name+" "+$stateparams.id) } }) .state('sixth', { url: '/sixth/detail/:name', templateurl: 'sixth.html' , //看到templateurl:后面包含了很多的模板 controller: function ($stateparams,$scope) { alert($stateparams.name) } }) /* .state('fourth', { url: '/fourth/:name', templateurl: 'third1.html' , //看到templateurl:后面包含了很多的模板 controller: function ($stateparams,$scope) { $scope.name=$stateparams.name; } }) */ }); </script> </body> </html>
下载地址:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。