最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

AngularJS使用ui-route實(shí)現(xiàn)多層嵌套路由的示例

 更新時(shí)間:2018年01月10日 08:35:41   作者:孟麗媛  
這篇文章主要介紹了AngularJS使用ui-route實(shí)現(xiàn)多層嵌套路由的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本文介紹了AngularJS使用ui-route實(shí)現(xiàn)多層嵌套路由的示例,分享給大家,具體如下:

一、預(yù)期實(shí)現(xiàn)效果:

https://liyuan-meng.github.io/uiRouter-app/index.html

 (項(xiàng)目地址:https://github.com/liyuan-meng/uiRouter-app

二、分析題目要求,給出依賴關(guān)系,構(gòu)建項(xiàng)目

1. service:

(1)根據(jù)條件查詢people數(shù)據(jù)checkPeople.service,不給出條件則查詢所有。

(2)得到路由信息getStateParams.service。

2. components:

(1)hello模塊:點(diǎn)擊button按鈕更改內(nèi)容。

(2)peolpleList模塊:顯示people列表,點(diǎn)擊people顯示people詳情。依賴于checkPeople.service模塊。

(3)peopleDetail模塊:顯示people詳情,依賴于checkPeople.service模塊和getStateParams.service模塊。

3. 構(gòu)建項(xiàng)目:

如圖所示:component目錄用來保存所有服務(wù)模塊和業(yè)務(wù)模塊,lib目錄保存外部引用(我是用的是angular.js1.5.8和ui-route0.2.18),app.config.js文件用來配置路由,index.html則作為入口文件。

三、實(shí)現(xiàn)這個(gè)例子

1. 首頁index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="./lib/angular.js"></script>
  <script src="./lib/angular-ui-route.js"></script>
  <script src="./app.config.js"></script>
  <script src="./components/core/people/checkPeople.service.js"></script>
  <script src="./components/core/people/getStateParams.service.js"></script>
  <script src="./components/hello/hello.component.js"></script>
  <script src="./components/people-list/people-list.component.js"></script>
  <script src="./components/people-detail/people-detail.component.js"></script>
</head>
<body ng-app="helloSolarSystem">
<div>
  <a ui-sref="helloState">Hello</a>
  <a ui-sref="aboutState">About</a>
  <a ui-sref="peopleState">People</a>
</div>

<ui-view></ui-view>

</body>
</html>

(1)導(dǎo)入lib中的文件以及所有用到的service和component服務(wù)的文件。

(2)ng-app="helloSolarSystem"指明了從helloSolarSystem模塊開始解析。

(3)定義視圖<ui-view></ui-view>

2. 配置路由app.config.js

'use strict';

angular.module("helloSolarSystem", ['peopleList', 'peopleDetail', 'hello','ui.router']).

  config(['$stateProvider', function ($stateProvider) {

    $stateProvider.state('helloState', {
      url: '/helloState',
      template:'<hello></hello>'

    }).state('aboutState', {
      url: '/about',
      template: '<h4>Its the UI-Router Hello Solar System app!</h4>'

    }).state('peopleState', {
      url: '/peopleList',
      template:'<people-list></people-list>'

    }).state('peopleState.details', {
      url:'/detail/:id',
      template: '<people-detail></people-detail>'
    })
  }
]);

(1)模塊名字:helloSolarSystem;

(2)注入'peopleList', 'peopleDetail', 'hello','ui.router'模塊。

(3)配置stateProvider服務(wù)的視圖控制,例如第一個(gè)名為helloState的視圖控制器:當(dāng)ui-sref == "helloState"的時(shí)候,路由更新為url的值#/helloState,并且<ui-view></ui-view>中顯示的內(nèi)容為<hello></hello>組件解析出的內(nèi)容。

(4)嵌套路由的實(shí)現(xiàn):名為peopleState的視圖控制器是父路由。名為peopleState.details的視圖控制器是子路由。這是一種相對(duì)路由方式,父路由將匹配.../index.html#/peopleState/,子路由將匹配.../index.html#/peopleState/detail/x(x是/detail/:id中的id的值)。如果改成絕對(duì)路由的形式,只需要寫成url:'^/detail/:id',這時(shí)子路由將匹配.../index.html#/detail/x(x是/detail/:id中的id的值)。

4. 實(shí)現(xiàn)checkPeople.service(根據(jù)條件查找people)

checkPeople.sercice.js

'use strict';

//根據(jù)條件(參數(shù))查找信息。
angular.module('people.checkPeople', ['ui.router']).
  factory('CheckPeople', ['$http', function ($http) {
    return {
      getData: getData
    };
    function getData(filed) {
      var people;
      var promise = $http({
        method: 'GET',
        url: './data/people.json'
      }).then(function (response) {
        if (filed) {
          people = response.data.filter(function (value) {
            if (Number(value.id) === Number(filed)) {
              return value;
            }
          })
        } else {
          people = response.data;
        }
        return people;
      });
      return promise;
    }
  }]);

(1)在getData這個(gè)函數(shù)中,我們想要返回一個(gè)保存people信息的數(shù)組,但是由于使用$http().then()服務(wù)的時(shí)候,這是一個(gè)異步請(qǐng)求,我們并不知道請(qǐng)求什么時(shí)候結(jié)束,所以世界返回people數(shù)組是有問題的。我們注意到,$http().then()是一個(gè)Promise對(duì)象,所以我們可以想到直接將這個(gè)對(duì)象返回,這樣在就可以使用"函數(shù)的結(jié)果.then(function(data))"來得到異步請(qǐng)求拿來的數(shù)據(jù)data。

3. 實(shí)現(xiàn)getStateParams.service(獲取路由信息)

getStatePatams.service.js

"use strict";

angular.module("getStateParams", ['ui.router']).
  factory("GetStateParams", ["$location", function ($location) {
    return {
      getParams: getParams
    };
    function getParams() {
      var partUrlArr = $location.url().split("/");
      return partUrlArr[partUrlArr.length-1];
    }
}]);

(1)這里的getParams函數(shù)返回的是路由信息的最后一個(gè)數(shù)據(jù),也就是people的id,這個(gè)service有些特殊,不夠通用,可能還需要優(yōu)化一下會(huì)更加合理。不過并不影響我們的需求。

4. 實(shí)現(xiàn)hello模塊

hello.template.html

<div>
  <div ng-hide="hideFirstContent">hello solar sytem!</div>
  <div ng-hide="!hideFirstContent">whats up solar sytem!</div>
  <button ng-click="ctlButton()">click</button>
</div>

hello.component.js

'use strict';

angular.module("hello", [])
  .component('hello', {
    templateUrl: './components/hello/hello.template.html',
    controller: ["$scope", 
      function HelloController($scope) {
        $scope.hideFirstContent = false;
        $scope.ctlButton = function () {
          this.hideFirstContent = !this.hideFirstContent;
        };
      }
    ]
  });

5. 實(shí)現(xiàn)peolpeList模塊:

peopleList.template.html

<div>
  <ul>
    <a ng-repeat="item in people" ui-sref="peopleState.details({id:item.id})">
      <li>{{item.name}}</li>
    </a>
  </ul>
  <ui-view></ui-view>
</div>

(1)這里的<ui-view></ui-view>用來顯示peopleList的子組件pepleDetail

peopleList.component.js

'use strict';

angular.module("peopleList", ['people.checkPeople'])
  .component('peopleList', {
    templateUrl: './components/people-list/people-list.template.html',
    controller: ['CheckPeople','$scope',
      function PeopleListController(CheckPeople, $scope) {
        $scope.people = [];
        CheckPeople.getData().then(function(data){
          $scope.people = data;
        });
      }
    ]
  });

6. 實(shí)現(xiàn)peopleDetail模塊

peopleDetail.template.html

<ul ng-repeat="item in peopleDetails track by $index">
  <li>名字: {{item.name}}</li>
  <li>介紹: {{item.intro}}</li>
</ul>

peopleDetail.component.js

'use strict';

angular.module("peopleDetail", ['people.checkPeople', 'getStateParams'])
  .component('peopleDetail', {
    templateUrl: './components/people-detail/people-detail.template.html',
    controller: ['CheckPeople', 'GetStateParams', '$scope',
      function peopleDetailController(CheckPeople, GetStateParams, $scope) {
        $scope.peopleDetails = [];
        CheckPeople.getData(GetStateParams.getParams()).then(function(data){
          $scope.peopleDetails = data;
        });
      }
    ]
  });

7.源碼:https://github.com/liyuan-meng/uiRouter-app

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • AngularJS利用Controller完成URL跳轉(zhuǎn)

    AngularJS利用Controller完成URL跳轉(zhuǎn)

    本文的主要內(nèi)容是介紹在AngularJS中怎樣利用Controller實(shí)現(xiàn)URL跳轉(zhuǎn),本文給出了實(shí)例代碼,簡單明了,有需要的可以參考學(xué)習(xí)。
    2016-08-08
  • angular中ui calendar的一些使用心得(推薦)

    angular中ui calendar的一些使用心得(推薦)

    下面小編就為大家?guī)硪黄猘ngular中ui calendar的一些使用心得(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • AngularJS入門教程(一):靜態(tài)模板

    AngularJS入門教程(一):靜態(tài)模板

    這篇文章主要介紹了AngularJS入門教程(一):靜態(tài)模板,本文是系列文章的第二篇,本系列會(huì)用一個(gè)項(xiàng)目來講解AngularJS的使用,需要的朋友可以參考下
    2014-12-12
  • AngularJS directive返回對(duì)象屬性詳解

    AngularJS directive返回對(duì)象屬性詳解

    這篇文章主要為大家纖細(xì)介紹了AngularJS directive返回對(duì)象屬性的相關(guān)內(nèi)容,感興趣的小伙伴們可以參考一下
    2016-03-03
  • AngularJS入門教程之ng-class 指令用法

    AngularJS入門教程之ng-class 指令用法

    本文主要介紹AngularJS ng-class 指令,這里幫大家整理了ng-class資料和示例代碼,學(xué)習(xí)AngularJS指令的同學(xué)可以參考下
    2016-08-08
  • AngualrJS中每次$http請(qǐng)求時(shí)的一個(gè)遮罩層Directive

    AngualrJS中每次$http請(qǐng)求時(shí)的一個(gè)遮罩層Directive

    AngularJS是一款非常強(qiáng)大的前端MVC框架。接下來通過本文給大家介紹AngualrJS中每次$http請(qǐng)求時(shí)的一個(gè)遮罩層Directive,本文非常具有參考借鑒價(jià)值,特此分享供大家學(xué)習(xí)
    2016-01-01
  • 利用Ionic2 + angular4實(shí)現(xiàn)一個(gè)地區(qū)選擇組件

    利用Ionic2 + angular4實(shí)現(xiàn)一個(gè)地區(qū)選擇組件

    ionic是一個(gè)移動(dòng)端開發(fā)框架,使用hybird技術(shù),只要使用前端開發(fā)技術(shù)就可以開發(fā)出電腦端,安卓端和ios端的站點(diǎn)程序。下面這篇文章主要給大家介紹了關(guān)于利用Ionic2 + angular4實(shí)現(xiàn)一個(gè)地區(qū)選擇組件的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-07-07
  • AngularJs directive詳解及示例代碼

    AngularJs directive詳解及示例代碼

    本文主要講解AngularJs directive的知識(shí),這里整理了相關(guān)資料及示例代碼,有興趣的小伙伴可以參考下
    2016-09-09
  • Angular?項(xiàng)目路徑添加指定的訪問前綴

    Angular?項(xiàng)目路徑添加指定的訪問前綴

    這篇文章主要為大家介紹了Angular?項(xiàng)目路徑添加指定的訪問前綴方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Angular使用過濾器uppercase/lowercase實(shí)現(xiàn)字母大小寫轉(zhuǎn)換功能示例

    Angular使用過濾器uppercase/lowercase實(shí)現(xiàn)字母大小寫轉(zhuǎn)換功能示例

    這篇文章主要介紹了Angular使用過濾器uppercase/lowercase實(shí)現(xiàn)字母大小寫轉(zhuǎn)換功能,涉及AngularJS過濾器針對(duì)字符串轉(zhuǎn)換的簡單使用技巧,需要的朋友可以參考下
    2018-03-03

最新評(píng)論

舟山市| 德兴市| 佛教| 高要市| 闸北区| 平乐县| 衡南县| 绥阳县| 长葛市| 瑞安市| 武平县| 前郭尔| 龙山县| 镇赉县| 河北省| 黄石市| 富阳市| 广汉市| 高邑县| 仙游县| 叙永县| 澳门| 溆浦县| 明光市| 军事| 清流县| 吴川市| 宜兰市| 万年县| 海丰县| 中宁县| 玉山县| 咸宁市| 于田县| 元朗区| 鹤壁市| 房山区| 沧州市| 南投市| 日喀则市| 卢湾区|