angularjs路由实现导航栏

angularjs路由实现导航栏

第一种方法:

<!DOCTYPE html>
<html ng-app="myApp">
	<head>
		<meta charset="UTF-8">
		<title>路由</title>
		<style>
			a{
				text-decoration:none; 
			}
		</style>
	</head>
	<body ng-controller="show">
			<div id="wei">
				<a href="#main">首页</a>
				<a href="#intro">公司简介</a>
				<a href="#about">关于</a>
				<a href="#contact">联系我们</a>
			</div>
			<div ng-view></div>
		<script src="//cdn.bootcss.com/angular.js/1.4.9/angular.min.js"></script>
		<script src="//cdn.bootcss.com/angular.js/1.4.9/angular-route.js"></script>

		<script type="text/javascript">
			var app = angular.module('myApp',["ngRoute"]);
			app.config(["$routeProvider",function($routeProvider){
				$routeProvider
				.when("/main",{
					template:"<p>首页{{name}}</p>",
						controller:"show"
				})
				.when("/intro",{
					template:"<p>公司简介{{name}}</p>",
					 controller:"show2"
				})
				.when("/about",{
					template:"<p>关于{{name}}</p>",
					 controller:"show3"
				})
				.when("/contact",{
					template:"<p>联系我们{{name}}</p>",
					controller:"show4"
				})
				// .otherwise({
				// 	redirectTo:"/main"
				// })
				.otherwise("/main")

			}])

			app.controller('show',['$scope','$http',function($scope,$http){
				$scope.name = "啦啦啦,我是卖报的小行家1!";
			}]);
			app.controller('show2',['$scope','$http',function($scope,$http){
				$scope.name = "啦啦啦,我是卖报的小行家2!";
			}]);
			app.controller('show3',['$scope','$http',function($scope,$http){
				$scope.name = "啦啦啦,我是卖报的小行家3!";
			}]);
			app.controller('show4',['$scope','$http',function($scope,$http){
				$scope.name = "啦啦啦,我是卖报的小行家4!";
			}]);
		</script>	
	</body>
</html>


第二种方法:

<!DOCTYPE html>
<html ng-app='myApp'>
	<head>
		<meta charset="UTF-8">
		<title>路由</title>
		<style>
			a{
				text-decoration:none; 
			}
		</style>
	</head>
	<body ng-controller='show'>
			<div id="wei">
				<a href="" ng-click="$location.path('main')">首页</a>
				<a href="" ng-click="$location.path('intro')">公司简介</a>
				<a href="" ng-click="$location.path('about')">关于</a>
				<a href="" ng-click="$location.path('contact')">联系我们</a>
			</div>
			<div ng-view></div>
		<script src="//cdn.bootcss.com/angular.js/1.4.9/angular.min.js"></script>
		<script src="//cdn.bootcss.com/angular.js/1.4.9/angular-route.js"></script>

		<script type="text/javascript">
			var app = angular.module('myApp',["ngRoute"]);
			app.config(["$routeProvider",function($routeProvider){
				$routeProvider
				.when("/main",{
					// template:"<p>首页{{name}}</p>",
					templateUrl:"main.html",
						controller:"show"
				})
				.when("/intro",{
					// template:"<p>公司简介{{name}}</p>",
					templateUrl:"intro.html",
					 controller:"show2"
				})
				.when("/about",{
					// template:"<p>关于{{name}}</p>",
					templateUrl:"about.html",
					 controller:"show3"
				})
				.when("/contact",{
					// template:"<p>联系我们{{name}}</p>",
					templateUrl:"contact.html",
					controller:"show4"
				})
				// .otherwise({
				// 	redirectTo:"/main"
				// })
				.otherwise("/main")

			}])

			app.controller('show',['$scope','$location',function($scope,$location){
				$scope.name = "啦啦啦,我是卖报的小行家1!";
				$scope.$location =$location;
			}]);
			app.controller('show2',['$scope','$location',function($scope,$location){
				$scope.name = "啦啦啦,我是卖报的小行家2!";
				$scope.$location =$location;
			}]);
			app.controller('show3',['$scope','$location',function($scope,$location){
				$scope.name = "啦啦啦,我是卖报的小行家3!";
				$scope.$location =$location;
			}]);
			app.controller('show4',['$scope','$location',function($scope,$location){
				$scope.name = "啦啦啦,我是卖报的小行家4!";
				$scope.$location =$location;
			}]);
		</script>	
	</body>
</html>


四个详情页为:

<p>首页你好{{name}}</p>
<p>首页你好{{name}}</p>
<p>关于你好{{name}}</p>
<p>联系我们你哈{{name}}</p>






  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值