很丑的小例子,剛學angularjs,寫下來方面以后看。
1.例子的工程目錄如下:

2.index.html代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="lib/angular.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="lib/angular-route.js"></script>
<title></title>
</head>
<body ng-app="app">
<h1>我的郵件</h1>
<!--模板(子視圖)將在這個地方插入-->
<div ng-view>
</div>
</body>
</html>
3.app.js內容:
var app = angular.module('app', ['ngRoute']);
//郵件
var messages=[{
id:0,
sender:"王經理",
subject:"項目總結",
date:"2015-4-2 09:00:4",
recipient:"小李",
message:"記得明天上午開會要收項目總結,不要搞砸了。"
},{
id:1,
sender:"小姨子",
subject:"明天吃飯",
date:"2015-4-2 23:12:56",
recipient:"小李",
message:"姐夫明天請我吃飯啦。"
}];
app.controller('emailList', ['$scope', function($scope){
$scope.emails=messages;
}]);
app.controller('emailDetail',['$scope','$routeParams',function($scope,$routeParams){
$scope.email=messages[$routeParams.id];
}]);
app.config(['$routeProvider',function($routeProvider) {
$routeProvider.when('/', {
controller:'emailList',
templateUrl:'./template/emailList.html'
//這個使用/view/:id,當路徑進行匹配的時候會自動解析出其中的id,可以通過$routeParams.id獲取。如:
// URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby // Route: /Chapter/:chapterId/Section/:sectionId // Then //$routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}
}).when('/view/:id',{
controller:'emailDetail',
templateUrl:'./template/emailDetail.html'
});
}]);
4.emailList.html和emailDetail.html內容:
<table>
<tr><td>發送者</td><td>主題</td><td>日期</td></tr>
<tr ng-repeat="email in emails">
<td>{{email.sender}}</td>
<td><a href="#/view/{{email.id}}">{{email.subject}}</a></td>
<td>{{email.date}}</td>
</tr>
</table>
<div>
<h2>主題:{{email.subject}}</h2>
<h3>發送者:{{email.sender}}</h3>
<h3>日期:{{email.date}}</h3>
<h3>接收者:{{email.recipient}}</h3>
<h2>內容:{{email.message}}</h2>
<h4><a href="#/"><<<返回</a></h4>
</div>
5.效果圖:


