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>