Angularjs制作簡(jiǎn)單的路由功能demo
從官網(wǎng)下載了最新版本的Angularjs 版本號(hào):1.3.15
做一個(gè)簡(jiǎn)單的路由功能demo
首頁(yè):index.html
<!DOCTYPE html > <html> <head> <meta charset="utf-8" /> <title>測(cè)試</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 = '測(cè)試顯示內(nèi)容'; }); //路由 function emailRouteConfig($routeProvider) { $routeProvider. when('/', { controller: ListController, templateUrl: 'list.html' }). when('/view/:id', { //在id前面加一個(gè)冒號(hào),從而制訂了一個(gè)參數(shù)化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,這是發(fā)送給您的郵件。" }, { id: 1, sender: "123456@qq.com", subject: "你好,這是一封郵件", date: "2015年4月13日", recipients: ['wifei@163.com'], message: "你好,我是xxx,這是發(fā)送給您的郵件。" }, { id: 2, sender: "123456@qq.com", subject: "你好,這是一封郵件", date: "2015年4月13日", recipients: ['wifei@163.com'], message: "你好,我是xxx,這是發(fā)送給您的郵件。" }]; function ListController($scope) { $scope.messages = messages; } function DetailController($scope,$routeParams) { $scope.message = messages[$routeParams.id]; } </script> </html>
列表頁(yè):list.html
<table> <tr> <td><strong>發(fā)件人</strong></td> <td><strong>內(nèi)容</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>
詳細(xì)頁(yè):detail.html
<div><strong>項(xiàng)目:</strong>{{message.subject}}</div> <div><strong>發(fā)送者:</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>
以下是此次demo的坑:
1:新版的Angularjs,引用路由功能,需要單獨(dú)再引用 angular-route.js 文件
2:在定義module時(shí)也需要添加對(duì)'ngRoute'的依賴
angular.module('xxxx', ['ngRoute'])
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- Angular+Node生成隨機(jī)數(shù)的方法
- angularjs實(shí)現(xiàn)猜數(shù)字大小功能
- angularjs實(shí)現(xiàn)猜大小功能
- AngularJS實(shí)現(xiàn)元素顯示和隱藏的幾個(gè)案例
- 使用AngularJS來(lái)實(shí)現(xiàn)HTML頁(yè)面嵌套的方法
- AngularJS中的指令全面解析(必看)
- AngularJS中的模塊詳解
- AngularJs 彈出模態(tài)框(model)
- angularjs 處理多個(gè)異步請(qǐng)求方法匯總
- AngularJS實(shí)現(xiàn)表單驗(yàn)證
- AngularJS實(shí)現(xiàn)的生成隨機(jī)數(shù)與猜數(shù)字大小功能示例
相關(guān)文章
Angular?結(jié)合?dygraphs?實(shí)現(xiàn)?annotation功能
這篇文章主要介紹了Angular?結(jié)合?dygraphs?實(shí)現(xiàn)?annotation,本文,我們直接結(jié)合 Angular 來(lái)演示,如何通過 dygraphs 實(shí)現(xiàn)折線圖上的 annotation 的功能,需要的朋友可以參考下2022-08-08AngularJS基礎(chǔ) ng-options 指令詳解
本文主要介紹AngularJS ng-options 指令,這里對(duì)ng-options指令的知識(shí)做了詳細(xì)整理,并附有詳細(xì)的代碼示例,有需要的小伙伴可以參考下2016-08-08Angular性能優(yōu)化之第三方組件和懶加載技術(shù)
這篇文章主要介紹了Angular性能優(yōu)化之第三方組件和懶加載技術(shù),對(duì)性能優(yōu)化感興趣的同學(xué),可以參考下2021-05-05cnpm加速Angular項(xiàng)目創(chuàng)建的方法
這篇文章主要介紹了cnpm加速Angular項(xiàng)目創(chuàng)建的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-09-09Angular中使用Api 代理的實(shí)現(xiàn)
我們對(duì)接的過程中總是遇到跨域的問題,本文使用 angualr 來(lái)講解代理api對(duì)接的話題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04詳解Angular的雙向數(shù)據(jù)綁定(MV-VM)
本文主要對(duì)Angular的雙向數(shù)據(jù)綁定(MV-VM)進(jìn)行實(shí)例分析,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2016-12-12Angular實(shí)踐之將Input與Lifecycle轉(zhuǎn)換成流示例詳解
這篇文章主要為大家介紹了Angular實(shí)踐之將Input與Lifecycle轉(zhuǎn)換成流示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02關(guān)于AngularJS中幾種Providers的區(qū)別總結(jié)
這篇文章主要給大家介紹了關(guān)于AngularJS中幾種Providers的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用AngularJS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Angular實(shí)現(xiàn)的敏感文字自動(dòng)過濾與提示功能示例
這篇文章主要介紹了Angular實(shí)現(xiàn)的敏感文字自動(dòng)過濾與提示功能,結(jié)合實(shí)例形式分析了AngularJS針對(duì)字符串的輸入判定及實(shí)時(shí)顯示相關(guān)操作技巧,需要的朋友可以參考下2017-12-12