angularJS的router用法簡單介紹

antzone發表於2017-04-01

本章節將分享一段關於angular的router程式碼例項,有需的朋友可以做一下參考。

下面就分別把相關程式碼列舉出來:

一.index.html程式碼:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="http://www.softwhy.com/" /> 
<title>螞蟻部落</title> 
</head>
<body ng-app="routeApp" class="ng-app:routeApp"  id="routeApp">
<script src="http://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.6.2pre/html5shiv.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/json2/20121008/json2.js"></script>
<div ng-view></div>
<script src="http://localhost:81/js/angular.min.js"></script>
<script>
var routeApp = angular.module('routeApp',[]);
routeApp.config(['$routeProvider',function ($routeProvider) {
  $routeProvider
    .when('/list', {
      templateUrl: 'list.html',
      controller: 'RouteListCtl'
    })
    .when('/list/:id', {
      templateUrl: 'detail.html',
      controller: 'RouteDetailCtl'
    })
    .otherwise({
      redirectTo: '/list'
    });
}]);
//controller
routeApp.controller('RouteListCtl',function($scope) {
});
routeApp.controller('RouteDetailCtl',function($scope, $routeParams) {
  $scope.id = $routeParams.id;
});
</script>
</body>
</html>

二list.html相關:

執行下面程式碼:

[HTML] 純文字檢視 複製程式碼
<hr/>
<h3>Route : List.html</h3>
<ul>
  <li ng-repeat="id in [1, 2, 3 ]"><a href="#/list/{{ id }}"> ID{{ id }}</a></li>
</ul>

三.detail.html相關:

執行以下程式碼:

[HTML] 純文字檢視 複製程式碼
<hr/>
<h3>Route <span style="color: red;">{{id}}</span>: detail.html </h3>