angular路由两种锚点

来源:互联网 发布:淘宝活动在哪里报名 编辑:程序博客网 时间:2024/06/16 09:40

1.第一种

<!DOCTYPE html>

<html ng-app="myApp">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body ng-controller="show">
<div>
<a href="" ng-click="$location.path('aaa')">首页</a>
<a href="" ng-click="$location.path('bbb')">详情页</a>
<a href="" ng-click="$location.path('ccc')">购物车</a>
<a href="" ng-click="$location.path('ddd')">个人中心</a>
</div>
<div ng-view></div>
</body>
<script src="http://cdn.bootcss.com/angular.js/1.4.9/angular.min.js"></script>
<script src="http://cdn.bootcss.com/angular.js/1.4.9/angular-route.js"></script>
<script type="text/javascript">
var my = angular.module('myApp',['ngRoute']);
my.config(['$routeProvider',function($routeProvider){
$routeProvider
.when('/aaa',{
template : '<p>这是我的首页,漂亮吧!{{text}}</p>',
controller : 'show'
})
.when('/bbb',{
template : '<p>这是我的详情页,漂亮吧!{{text}}</p>',
controller : 'hide'
})
.when('/ccc',{
template : '<p>这是我的购物车,漂亮吧!{{text}}</p>',
controller : 'showhide'
})
.when('/ddd',{
template : '<p>这是我的个人中心,漂亮吧!{{text}}</p>',
controller : 'hideshow'
})
.otherwise("/aaa")
}])
my.controller('show',['$scope','$location',function($scope,$location){
$scope.text = '111';
$scope.$location = $location;
}]);
my.controller('hide',['$scope','$location',function($scope,$location){
$scope.text = '222';
$scope.$location = $location;
}]);
my.controller('showhide',['$scope','$location',function($scope,$location){
$scope.text = '333';
$scope.$location = $location;
}]);
my.controller('hideshow',['$scope','$location',function($scope,$location){
$scope.text = '444';
$scope.$location = $location;
}]);
</script>

</html>

2.第二种

<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body ng-controller="show">
<div>
<a href="#aaa">首页</a>
<a href="#bbb">详情页</a>
<a href="#ccc">购物车</a>
<a href="#ddd">个人中心</a>
</div>
<div ng-view></div>
</body>
<script src="http://cdn.bootcss.com/angular.js/1.4.9/angular.min.js"></script>
<script src="http://cdn.bootcss.com/angular.js/1.4.9/angular-route.js"></script>
<script type="text/javascript">
var my = angular.module('myApp',['ngRoute']);
my.config(['$routeProvider',function($routeProvider){
$routeProvider
.when('/aaa',{
template : '<p>这是我的首页,漂亮吧!{{text}}</p>',
controller : 'show'
})
.when('/bbb',{
template : '<p>这是我的详情页,漂亮吧!{{text}}</p>',
controller : 'hide'
})
.when('/ccc',{
template : '<p>这是我的购物车,漂亮吧!{{text}}</p>',
controller : 'showhide'
})
.when('/ddd',{
template : '<p>这是我的个人中心,漂亮吧!{{text}}</p>',
controller : 'hideshow'
})
.otherwise({
redirectTo : '/aaa'
})
}])
my.controller('show',['$scope',function($scope){
$scope.text = '111'
}]);
my.controller('hide',['$scope',function($scope){
$scope.text = '222'
}]);
my.controller('showhide',['$scope',function($scope){
$scope.text = '333'
}]);
my.controller('hideshow',['$scope',function($scope){
$scope.text = '444'
}]);
</script>
</html>

0 0
原创粉丝点击