国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

AngularJS監(jiān)聽路由變化的方法

2024-05-06 15:10:13
字體:
供稿:網(wǎng)友

使用AngularJS時,當(dāng)路由發(fā)生改變時,我們需要做某些處理,此時可以監(jiān)聽路由事件,常用的是$routeStartChange, $routeChangeSuccess。完整例子如下:

<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AngularJS監(jiān)聽路由變化</title></head><body ng-app="ngRouteExample">  <div id="navigation">    <a href="#/home" rel="external nofollow" >Home</a>   <a href="#/about" rel="external nofollow" >About</a>  </div>     <div ng-view></div>  <script type="text/ng-template" id="home.html">   <h1> Home </h1>   <table>    <tbody>     <tr ng-repeat="x in records" style="background:#abcdef;">      <td>{{x.Name}}</td>      <td>{{x.Country}}</td>      </tr>         </tbody>   </table>  </script>  <script type="text/ng-template" id="about.html">   <h1> About </h1>   <p>在輸入框中嘗試輸入:</p>   <p>姓名:<input type="text" ng-model="name"></p>   <p>你輸入的為: {{name}}</p>  </script>  <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>  <script src="http://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.js"></script>  <script type="text/javascript">  angular.module('ngRouteExample', ['ngRoute'])  .config(function ($routeProvider) {    $routeProvider.    when('/home', {      templateUrl: 'home.html',      controller: 'HomeController'    }).    when('/about', {      templateUrl: 'about.html',      controller: 'AboutController'    }).    otherwise({      redirectTo: '/home'    });  })  .run(['$rootScope', '$location', function($rootScope, $location) {    /* 監(jiān)聽路由的狀態(tài)變化 */    $rootScope.$on('$routeChangeStart', function(evt, next, current){     console.log('route begin change');    });     $rootScope.$on('$routeChangeSuccess', function(evt, current, previous) {     console.log('route have already changed :'+$location.path());    });   }])  .controller('HomeController', function ($scope) {     $scope.records = [{     "Name" : "Alfreds Futterkiste",     "Country" : "Germany"    },{     "Name" : "Berglunds snabbköp",     "Country" : "Sweden"    },{     "Name" : "Centro comercial Moctezuma",     "Country" : "Mexico"    },{     "Name" : "Ernst Handel",     "Country" : "Austria"    }]  })      .controller('AboutController', function ($scope) {    $scope.name = '呵呵';  });</script>  </body></html>

上述的例子是AngularJS 1的,對于Angular2是否也可以用,還沒嘗試過,有機會驗證了再記錄下咯~~

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 濮阳市| 会宁县| 凤庆县| 东乌珠穆沁旗| 建阳市| 蚌埠市| 望谟县| 本溪市| 玉环县| 松原市| 兴国县| 沙坪坝区| 宜兰市| 锦屏县| 岱山县| 鄂托克旗| 包头市| 西畴县| 寻甸| 嘉祥县| 古交市| 西宁市| 云霄县| 天等县| 湘阴县| 息烽县| 沙坪坝区| 阿尔山市| 湘乡市| 云梦县| 岳阳县| 吉安县| 延边| 京山县| 永定县| 利川市| 梁河县| 陆川县| 会理县| 广东省| 昂仁县|