欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular路由ui-router配置詳解

 更新時(shí)間:2018年08月01日 11:07:46   作者:全村的希望iOS  
這篇文章主要介紹了Angular路由ui-router配置詳解,非常不錯(cuò),具有一定得參考借鑒價(jià)值,需要的朋友參考下吧

簡(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單選框相關(guān)的示例代碼

    Angularjs單選框相關(guān)的示例代碼

    本篇文章主要介紹了Angularjs單選框相關(guān)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁(yè)實(shí)例代碼

    AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁(yè)實(shí)例代碼

    這篇文章主要介紹了AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁(yè)的相關(guān)資料,并附實(shí)例代碼和實(shí)現(xiàn)效果圖,需要的朋友可以參考下
    2016-10-10
  • 淺談angular2子組件的事件傳遞(任意組件事件傳遞)

    淺談angular2子組件的事件傳遞(任意組件事件傳遞)

    今天小編就為大家分享一篇淺談angular2子組件的事件傳遞(任意組件事件傳遞),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • AngularJs Managing Service Dependencies詳解

    AngularJs Managing Service Dependencies詳解

    本站主要介紹AngularJs Managing Service Dependencies的知識(shí)資料,這里整理相關(guān)知識(shí),及簡(jiǎn)單示例代碼,有興趣的小伙伴可以參考下
    2016-09-09
  • AngularJS基礎(chǔ)學(xué)習(xí)筆記之表達(dá)式

    AngularJS基礎(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
  • AngularJS自動(dòng)表單驗(yàn)證

    AngularJS自動(dòng)表單驗(yàn)證

    這篇文章主要介紹了AngularJS手動(dòng)表單驗(yàn)證的相關(guān)資料,AngularJS的表單驗(yàn)證大致有兩種,一種是手動(dòng)驗(yàn)證,一種是自動(dòng)驗(yàn)證,本文重點(diǎn)介紹AngularJS自動(dòng)表單驗(yàn)證,感興趣的小伙伴們可以參考一下
    2016-02-02
  • 使用RxJS更優(yōu)雅地進(jìn)行定時(shí)請(qǐng)求詳析

    使用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
  • 深入理解AngularJs-scope的臟檢查(一)

    深入理解AngularJs-scope的臟檢查(一)

    這篇文章主要介紹了深入理解AngularJs-scope的臟檢查(一) ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • Angularjs中使用指令綁定點(diǎn)擊事件的方法

    Angularjs中使用指令綁定點(diǎn)擊事件的方法

    本篇文章主要介紹了Angularjs中使用指令綁定點(diǎn)擊事件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Angular 中 select指令用法詳解

    Angular 中 select指令用法詳解

    這篇文章主要介紹了Angular 中 select指令用法詳解的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09

最新評(píng)論