angular.js 路由及頁面?zhèn)鲄⑹纠?/h1>
更新時(shí)間:2017年02月24日 15:11:29 作者:GOODDEEP
本篇文章主要介紹了angular.js 路由及頁面?zhèn)鲄⑹纠【幱X得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
頁面?zhèn)鲄?shù)方法:1、$rootScope。2、(url)/user/:name/:age。
頁面轉(zhuǎn)換方法: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 路由實(shí)例</title>
</head>
<body ng-app='routingDemoApp' ng-controller="myCtrl">
<h2>AngularJS 路由應(yīng)用</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" >打印機(jī)</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){
//響應(yīng)成功
$scope.names = data[0].age;
$rootScope.name="rrrrrr";
}).error(function(data,header,config,status){
//處理響應(yīng)失敗
});
});
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:'這是打印機(jī)頁面'})
.when('/second_2',{template:'這是second_2'})
.otherwise({redirectTo:'/'});
}]);
</script>
</body>
</html>
(2)ui-router
<html>
<head>
<meta charset="utf-8">
<title>AngularJS 路由實(shí)例 </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傳參數(shù)</a>
<a ui-sref="fifth({'name':123,'id':256})">ui-sref傳參數(shù)</a>
<button ng-click="ngclick_go()" class="btn btn-primary " >state.go傳參數(shù)</button>
<button ng-click="ngclick_location()" class="btn btn-primary " >location傳參數(shù)</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}); // 跳轉(zhuǎn)后的URL: #/camnpr/appManager
};
$scope.ngclick_location = function() {
$location.path('/sixth/detail/42'); // 功能也是跳轉(zhuǎn)的
};
});
app.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/second'); //與原生的$routerProvider寫法不一樣的就是$urlRouterProvider先寫默認(rèn)路徑
$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>
下載地址:angu_demo_jb51.rar
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關(guān)文章
-
Angular.Js中ng-include指令的使用與實(shí)現(xiàn)
ng-include 指令用于包含外部的 HTML 文件。包含的內(nèi)容將作為指定元素的子節(jié)點(diǎn)。下面這篇文章主要給大家介紹了Angular.Js中ng-include指令的使用與實(shí)現(xiàn)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們下面來一起看看吧。 2017-05-05
-
angular ngClick阻止冒泡使用默認(rèn)行為的方法
這篇文章主要介紹了angular ngClick阻止冒泡使用默認(rèn)行為的方法,較為詳細(xì)的分析了AngularJS中ngClick事件執(zhí)行原理與阻止冒泡的實(shí)現(xiàn)技巧,需要的朋友可以參考下 2016-11-11
-
Angular.js中$apply()和$digest()的深入理解
相信大家都知道$digest()和$apply()是AngularJS中的兩個(gè)核心并且有時(shí)候容易引人誤解的部分。我們需要深入理解這兩者是如何運(yùn)作的,從而才能理解AngularJS本身是如何運(yùn)作的。本文的目的就是介紹$digest()和$apply()是如何確確實(shí)實(shí)的對(duì)你有用的。下面來一起看看吧。 2016-10-10
-
解決angularjs service中依賴注入$scope報(bào)錯(cuò)的問題
今天小編就為大家分享一篇解決angularjs service中依賴注入$scope報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧 2018-10-10
-
Angular.js中上傳指令ng-upload的基本使用教程
這篇文章主要給大家介紹了關(guān)于Angular.js中上傳指令ng-upload的基本使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。 2017-07-07
-
AngularJS標(biāo)簽頁tab選項(xiàng)卡切換功能經(jīng)典實(shí)例詳解
這篇文章主要介紹了AngularJS實(shí)現(xiàn)標(biāo)簽頁tab選項(xiàng)卡功能,結(jié)合實(shí)例形式總結(jié)分析了各種常用的tab選項(xiàng)卡切換操作實(shí)現(xiàn)技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下 2018-05-05
-
基于AngularJS實(shí)現(xiàn)頁面滾動(dòng)到底自動(dòng)加載數(shù)據(jù)的功能
本文主要給大家介紹基于AngularJS實(shí)現(xiàn)頁面滾動(dòng)到底自動(dòng)加載數(shù)據(jù)的功能,通過第三方控件來實(shí)現(xiàn),感興趣的朋友跟著小編一起看看具體實(shí)現(xiàn)代碼吧 2015-10-10
最新評(píng)論
頁面?zhèn)鲄?shù)方法:1、$rootScope。2、(url)/user/:name/:age。
頁面轉(zhuǎn)換方法: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 路由實(shí)例</title> </head> <body ng-app='routingDemoApp' ng-controller="myCtrl"> <h2>AngularJS 路由應(yīng)用</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" >打印機(jī)</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){ //響應(yīng)成功 $scope.names = data[0].age; $rootScope.name="rrrrrr"; }).error(function(data,header,config,status){ //處理響應(yīng)失敗 }); }); 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:'這是打印機(jī)頁面'}) .when('/second_2',{template:'這是second_2'}) .otherwise({redirectTo:'/'}); }]); </script> </body> </html>
(2)ui-router
<html> <head> <meta charset="utf-8"> <title>AngularJS 路由實(shí)例 </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傳參數(shù)</a> <a ui-sref="fifth({'name':123,'id':256})">ui-sref傳參數(shù)</a> <button ng-click="ngclick_go()" class="btn btn-primary " >state.go傳參數(shù)</button> <button ng-click="ngclick_location()" class="btn btn-primary " >location傳參數(shù)</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}); // 跳轉(zhuǎn)后的URL: #/camnpr/appManager }; $scope.ngclick_location = function() { $location.path('/sixth/detail/42'); // 功能也是跳轉(zhuǎn)的 }; }); app.config(function($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise('/second'); //與原生的$routerProvider寫法不一樣的就是$urlRouterProvider先寫默認(rèn)路徑 $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>
下載地址:angu_demo_jb51.rar
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angular.Js中ng-include指令的使用與實(shí)現(xiàn)
ng-include 指令用于包含外部的 HTML 文件。包含的內(nèi)容將作為指定元素的子節(jié)點(diǎn)。下面這篇文章主要給大家介紹了Angular.Js中ng-include指令的使用與實(shí)現(xiàn)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們下面來一起看看吧。2017-05-05angular ngClick阻止冒泡使用默認(rèn)行為的方法
這篇文章主要介紹了angular ngClick阻止冒泡使用默認(rèn)行為的方法,較為詳細(xì)的分析了AngularJS中ngClick事件執(zhí)行原理與阻止冒泡的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-11-11Angular.js中$apply()和$digest()的深入理解
相信大家都知道$digest()和$apply()是AngularJS中的兩個(gè)核心并且有時(shí)候容易引人誤解的部分。我們需要深入理解這兩者是如何運(yùn)作的,從而才能理解AngularJS本身是如何運(yùn)作的。本文的目的就是介紹$digest()和$apply()是如何確確實(shí)實(shí)的對(duì)你有用的。下面來一起看看吧。2016-10-10解決angularjs service中依賴注入$scope報(bào)錯(cuò)的問題
今天小編就為大家分享一篇解決angularjs service中依賴注入$scope報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10Angular.js中上傳指令ng-upload的基本使用教程
這篇文章主要給大家介紹了關(guān)于Angular.js中上傳指令ng-upload的基本使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07AngularJS標(biāo)簽頁tab選項(xiàng)卡切換功能經(jīng)典實(shí)例詳解
這篇文章主要介紹了AngularJS實(shí)現(xiàn)標(biāo)簽頁tab選項(xiàng)卡功能,結(jié)合實(shí)例形式總結(jié)分析了各種常用的tab選項(xiàng)卡切換操作實(shí)現(xiàn)技巧與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-05-05基于AngularJS實(shí)現(xiàn)頁面滾動(dòng)到底自動(dòng)加載數(shù)據(jù)的功能
本文主要給大家介紹基于AngularJS實(shí)現(xiàn)頁面滾動(dòng)到底自動(dòng)加載數(shù)據(jù)的功能,通過第三方控件來實(shí)現(xiàn),感興趣的朋友跟著小編一起看看具體實(shí)現(xiàn)代碼吧2015-10-10