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>首頁</h1>"
}) .state("about",{
url: "/about"
template: "關(guān)于我們"
});
$urlRouterProvider.otherwise("home")
}])
頁面配置
<div ui-view></div> //相當(dāng)于Vue中的插槽,單頁面應(yīng)用切換路由用來顯示當(dāng)前路由界面 <a ui-sref="home">首頁</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">首頁</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: "首頁"
}) .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>
嵌套路由頁面
<div>
<h1>商品展示</h1>
<button ng-click="jump()">點(diǎn)擊跳轉(zhuǎn)首頁</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使用gulp-uglify壓縮后執(zhí)行報(bào)錯(cuò)的解決方法
下面小編就為大家分享一篇angularjs使用gulp-uglify壓縮后執(zhí)行報(bào)錯(cuò)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Angular 數(shù)據(jù)請(qǐng)求的實(shí)現(xiàn)方法
本篇文章主要介紹了Angular 數(shù)據(jù)請(qǐng)求的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
AngularJS 中的指令實(shí)踐開發(fā)指南(一)
指令(Directives)是所有AngularJS應(yīng)用最重要的部分。盡管AngularJS已經(jīng)提供了非常豐富的指令,但還是經(jīng)常需要?jiǎng)?chuàng)建應(yīng)用特定的指令。這篇教程會(huì)為你講述如何自定義指令,以及介紹如何在實(shí)際項(xiàng)目中使用2016-03-03
AngularJS $http模塊POST請(qǐng)求實(shí)現(xiàn)
本篇文章主要介紹了AngularJS $http模塊POST請(qǐng)求實(shí)現(xiàn),這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Angularjs中$http以post請(qǐng)求通過消息體傳遞參數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了Angularjs中$http以post請(qǐng)求通過消息體傳遞參數(shù)的方法,結(jié)合實(shí)例形式分析了$http使用post請(qǐng)求傳遞參數(shù)的相關(guān)設(shè)置與使用技巧,需要的朋友可以參考下2016-08-08
Angular.JS內(nèi)置服務(wù)$http對(duì)數(shù)據(jù)庫的增刪改使用教程
我們可以使用內(nèi)置的$http服務(wù)直接同外部進(jìn)行通信。$http服務(wù)只是簡(jiǎn)單的封裝了瀏覽器原生的XMLHttpRequest對(duì)象,下面這篇文章主要給大家介紹了關(guān)于Angular.JS內(nèi)置服務(wù)$http對(duì)數(shù)據(jù)庫的增刪改等操作的相關(guān)資料,需要的朋友可以參考下。2017-05-05

