Angular路由ui-router配置詳解
簡(jiǎn)介
angularJs自身提供路由ng-router,但是ng-router不是很好用,配置項(xiàng)零散,好比Vue提供的組件傳值一樣,雖然提供給你了用法,但是開發(fā)過程中邏輯一多用著萌萌的,所以我們拋開ng-router來看ui-router。
引入ui-router
我們可以去bootCDN搜索ui-router,本地創(chuàng)建js文件,將代碼copy進(jìn)去使用,這樣就可以打入本地使用了,但是要注意的是,Angular的main.js一定要在ui-router之前引用,注意一下先后順序問題。
例如:
<script src="angular.main.js"></script> <script src="angular-ui-router.js"></script>
配置ui-router
//angular.module("moduleName",dep); 定義模塊依賴(兩個(gè)參數(shù)) //angular.module("moduleName"); 獲取模塊 (一個(gè)參數(shù)) var app = angular.module("myApp",["ui-router"]); app.config(["$stateProvider","$urlRouterProvider",function($stateProvider){ //app.config配置項(xiàng) //$stateProvider 狀態(tài)供應(yīng)商,(名字可以看出關(guān)于路由的一系列配置需要由$stateProvider完成) //$urlRouterProvider 路由重定向 $stateProvider.state("home",{ url: "/home" template: "<h1>首頁(yè)</h1>" }) .state("about",{ url: "/about" template: "關(guān)于我們" }); $urlRouterProvider.otherwise("home") }])
頁(yè)面配置
<div ui-view></div> //相當(dāng)于Vue中的插槽,單頁(yè)面應(yīng)用切換路由用來顯示當(dāng)前路由界面 <a ui-sref="home">首頁(yè)</a> //Angular默認(rèn)會(huì)轉(zhuǎn)換為href <a ui-sref="about">關(guān)于我們</a> //Angular默認(rèn)會(huì)轉(zhuǎn)換為href
路由激活狀態(tài)樣式
ui-sref-active="active"
完整代碼
<html ng-app="myApp"> <head> <style> .active{ color: red } </style> <script src="angular.main.js"></script> <script src="angular-ui-router.js"></script> </head> <body> <div ui-view></div> <footer> <a ui-sref="home" ui-sref-active="active">首頁(yè)</a> <a ui-sref="about" ui-sref-active="active">關(guān)于</a> <a ui-sref="items">商品</a> </footer> </body> <script> var app = angular.module("myApp", [ui-router]); app.config(["$stateProvider","$urlRouterProvider",function($stateProvider){ $stateProvider.state("home",{ url: "/home" template: "首頁(yè)" }) .state("about",{ url: "/about" template: "關(guān)于我們" }).state("items",{//牛逼的潛逃路由 url: "/items", templateUrl: "./items.html", controller:["$scope",$state,function($scope,$state){ $scope.jump = function(){ $state.go("home"); } $scope.jumpOther = function() { $state.go("items.phone",{ id: "phone" }); } }] }).state("items.comp",{ url: "/comp", template: "<h1>電腦商品</h1>" }).state("item.phone",{ url:"phone/:id", template:"<h1>手機(jī)商品</h1>", controller:["$scope","$stateParams",function($scope,$stateParams){ console.log($stateParams); }] }); $urlRouterProvider.otherwise("home") } </script> </html>
嵌套路由頁(yè)面
<div> <h1>商品展示</h1> <button ng-click="jump()">點(diǎn)擊跳轉(zhuǎn)首頁(yè)</button> <a ui-sref="about">跳轉(zhuǎn)至關(guān)于我們</a> <button ng-click="jumpOther()">穿參數(shù)</button> <a ui-sref="items.other({id:"sref"})"></a> <ul> //因?yàn)槲覀兺饷娓讣?jí)路由是items所以自路由用items為前綴 <li><a ui-sref="items.comp">電腦</a></li> <li><a ui-sref="items.phone">手機(jī)</a></li> </ul> <div ui-view></div> </div>
總結(jié)
以上所述是小編給大家介紹的Angular路由ui-router配置詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁(yè)實(shí)例代碼
這篇文章主要介紹了AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁(yè)的相關(guān)資料,并附實(shí)例代碼和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-10-10AngularJs Managing Service Dependencies詳解
本站主要介紹AngularJs Managing Service Dependencies的知識(shí)資料,這里整理相關(guān)知識(shí),及簡(jiǎn)單示例代碼,有興趣的小伙伴可以參考下2016-09-09AngularJS基礎(chǔ)學(xué)習(xí)筆記之表達(dá)式
AngularJS表達(dá)式用于應(yīng)用程序數(shù)據(jù)綁定到HTML。表達(dá)式都寫在雙括號(hào)就像{{表達(dá)式}}。表達(dá)式中的行為跟ng-bind指令方式相同。 AngularJS應(yīng)用表達(dá)式是純javascript表達(dá)式,并輸出它們被使用的數(shù)據(jù)在那里。2015-05-05使用RxJS更優(yōu)雅地進(jìn)行定時(shí)請(qǐng)求詳析
這篇文章主要給大家介紹了關(guān)于如何使用RxJS更優(yōu)雅地進(jìn)行定時(shí)請(qǐng)求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用RxJS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06