angularJs 简单实例

来源:互联网 发布:windows xp 64位原版 编辑:程序博客网 时间:2024/06/06 02:46

入口 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


0 0
原创粉丝点击