网络编程 发布日期:2025/11/11 浏览次数:1
使用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="/UploadFiles/2021-04-02/angular.min.js">
上述的例子是AngularJS 1的,对于Angular2是否也可以用,还没尝试过,有机会验证了再记录下咯~~
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。