angularJs 簡單實例

入口 agtest.html


<!DOCTYPE html>
<html style="width: 100%; height: 100%;">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<title>Insert title here</title>
<script type="text/javascript" src="lib/angular-1.4.0/angular.min.js"></script>
<script type="text/javascript" src="lib/angular-1.4.0/angular-route.js"></script>
<script src="testApp.js"></script>
</head>
<body ng-app="testApp" style="width: 100%;height: 100%;">
<ul>
 <li><a href="#a">A</a>
 <li><a href="#b">B</a>
</ul>
<div ng-view style="width: 100%;height: 100%;">
 
</div>
</body>
</html>


testApp.js

/**獲得全局app對象*/
var testApp = angular.module('testApp', [ 'ngRoute']);


/**配置路由器*/
testApp.config(function($routeProvider,$locationProvider,$httpProvider) {
$routeProvider
.when('/a', {
templateUrl : 'a.html',
controller : 'AController'
})


.when('/b', {
templateUrl : 'b.html',
controller : 'BController'
})
});



testApp.controller('AController', function($scope) {
$scope.info="Page A";
});


testApp.controller('BController', function($scope) {
$scope.info="Page B";
});


a.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<title>Insert title here</title>
</head>
<body style="width: 100%;height: 100%;">
  <label style="color: #f00;">{{info}}</label>
</body>
</html>

b.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<title>Insert title here</title>
</head>
<body style="width: 100%;height: 100%;color: #f00;" >
  <label style="color: #f0f;">{{info}}</label>
</body>
</html>


運行結果:

點擊A


點擊B


發佈了30 篇原創文章 · 獲贊 21 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章