Rumah >hujung hadapan web >tutorial js >Angularjs membuat fungsi penghalaan mudah demo_AngularJS
Memuat turun versi terkini Angularjs dari tapak web rasmi Nombor versi: 1.3.15
Buat demo fungsi penghalaan mudah
Halaman utama: index.html
<!DOCTYPE html > <html> <head> <meta charset="utf-8" /> <title>测试</title> <script src="./js/angular.min.js"></script> <script src="./js/angular-route.min.js"></script> </head> <body ng-app="myApp"> <div ng-controller="TextController"> <p>{{someText}}</p> </div> <div ng-view></div> </body> <script> var myApp = angular.module('myApp', ['ngRoute']); myApp.controller('TextController', function ($scope) { $scope.someText = '测试显示内容'; }); //路由 function emailRouteConfig($routeProvider) { $routeProvider. when('/', { controller: ListController, templateUrl: 'list.html' }). when('/view/:id', { //在id前面加一个冒号,从而制订了一个参数化URL controller: DetailController, templateUrl: 'detail.html' }). otherwise({ redirectTo: '/' }); } myApp.config(emailRouteConfig);//配置我们的路由 messages = [{ id: 0, sender: "123456@qq.com", subject: "你好,这是一封邮件", date: "2015年4月13日", recipients: ['wifei@163.com'], message: "你好,我是xxx,这是发送给您的邮件。" }, { id: 1, sender: "123456@qq.com", subject: "你好,这是一封邮件", date: "2015年4月13日", recipients: ['wifei@163.com'], message: "你好,我是xxx,这是发送给您的邮件。" }, { id: 2, sender: "123456@qq.com", subject: "你好,这是一封邮件", date: "2015年4月13日", recipients: ['wifei@163.com'], message: "你好,我是xxx,这是发送给您的邮件。" }]; function ListController($scope) { $scope.messages = messages; } function DetailController($scope,$routeParams) { $scope.message = messages[$routeParams.id]; } </script> </html>
Halaman senarai: list.html
<table> <tr> <td><strong>发件人</strong></td> <td><strong>内容</strong></td> <td><strong>日期</strong></td> </tr> <tr ng-repeat="message in messages"> <td>{{message.sender}}</td> <td><a href="#/view/{{message.id}}">{{message.subject}}</a></td> <td>{{message.date}}</td> </tr> </table>
Halaman butiran: detail.html
<div><strong>项目:</strong>{{message.subject}}</div> <div><strong>发送者:</strong>{{message.sender}}</div> <div><strong>日期:</strong>{{message.date}}</div> <div> <strong>To:</strong> <span ng-repeat="recipient in message.recipients"> {{recipient}} </span> </div> <div>{{message.message}}</div> <a href="#/">回到列表</a>
Berikut ialah perangkap demo ini:
1: Versi baharu Angularjs, untuk merujuk fungsi penghalaan, perlu merujuk fail angular-route.js secara berasingan
2: Anda juga perlu menambah kebergantungan pada 'ngRoute' semasa mentakrifkan modul
angular.module('xxxx', ['ngRoute'])
Di atas adalah keseluruhan kandungan artikel ini, saya harap anda semua menyukainya.