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

首頁 > 編程 > JavaScript > 正文

AngularJS監聽路由變化的方法

2019-11-19 17:17:09
字體:
來源:轉載
供稿:網友

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

<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>AngularJS監聽路由變化</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) {    /* 監聽路由的狀態變化 */    $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是否也可以用,還沒嘗試過,有機會驗證了再記錄下咯~~

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 芮城县| 江永县| 图们市| 文登市| 涞水县| 勐海县| 永州市| 昔阳县| 丰顺县| 威远县| 阜平县| 紫阳县| 乌恰县| 常州市| 天镇县| 博客| 绩溪县| 滨海县| 禄丰县| 东台市| 什邡市| 桦南县| SHOW| 义乌市| 蒲城县| 交城县| 洪洞县| 锡林浩特市| 榆林市| 米林县| 昌吉市| 大方县| 商南县| 绍兴县| 同德县| 万源市| 涪陵区| 长泰县| 沅陵县| 九江市| 千阳县|