您的位置:首页 > Web前端 > AngularJS

angularJs 简单实例

2017-01-22 14:44 274 查看
入口 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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  angularJS