angularJS的router用法简单介绍

我心飞翔 分类:实例代码

本章节将分享一段关于angular的router代码实例,有需的朋友可以做一下参考。

下面就分别把相关代码列举出来:

一.index.html代码:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset=" utf-8"> 
<meta name="author" content="http://www.pipipi.net/" /> 
<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相关:

运行下面代码:

<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相关:

运行以下代码:

<hr/>
<h3>Route <span style="color: red;">{{id}}</span>: detail.html </h3>
网站出售中,有意者加微信:javadudu

回复

我来回复
  • 暂无回复内容