8.6 服务汇总篇
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AngularJS 自定义服务</title>
</head>
<body ng-app="App">
<div ng-controller="DemoController">
<!-- -->
</div>
<script src="./libs/angular.min.js"></script>
<script>
var App = angular.module('App', []);
App.controller('DemoController', ['$scope', '$http', 'format', function ($scope, $http, format) {
var data = {
name: 'itcast',
age: 10
};
// 测试自定义的服务
// console.log(format(data));
console.log(format.format(data));
$http({
url: 'example.php',
method: 'post',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: format.format(data)
}).success(function (info) {
// info
console.log(info);
});
}]);
// 提供了3种方法实现自定义服务
// factory、service、value
App.factory('format', ['$filter', function ($filter) {
// 自定义服务,但依赖于$filter
// 定义服务具体功能
function format(arg) {
var s = '';
for(var key in arg) {
s += key + '=' + arg[key] + '&';
}
s = s.slice(0, -1);
return s;
}
// 具体功能
function sayHello() {
alert('你好')
}
// 返回出去以被调用
return {
format: format,
sayHello: sayHello
}
}]);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
<meta charset="UTF-8">
<title>AngularJS 自定义服务</title>
</head>
<body>
<div ng-controller="DemoController">
<h1>{{now}}</h1>
</div>
<script src="./libs/angular.min.js"></script>
<script>
var App = angular.module('App', []);
App.controller('DemoController', ['$scope', 'showTime', function ($scope, showTime) {
$scope.now = showTime.now();
showTime.sayHello();
}]);
// 自定义服务显示日期
// 提供了3种方法实现自定义服务
// factory、service、value
App.service('showTime', ['$filter', function ($filter) {
// return {
// showTime: function () {
// }
// }
var now = new Date;
var date = $filter('date');
// 向对象上添加具体方法
this.now = function () {
return date(now, 'yyyy/MM/dd hh:mm:ss');
}
// 添加具体方法
this.sayHello = function () {
alert('你好');
}
}]);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
<meta charset="UTF-8">
<title>AngularJS 自定义服务</title>
</head>
<body>
<div ng-controller="DemoController">
{{author}}
{{version}}
</div>
<script src="./libs/angular.min.js"></script>
<script>
var App = angular.module('App', []);
// 自定义常量服务
// 提供了3种方法实现自定义服务
// factory、service、value
App.value('author', 'zhaoyc');
App.value('version', '1.0');
// 本质上一个服务
// 从表现形式上是一个常量
// 常量就是不变的值与变对量相对应
// 声明依赖调用服务
App.controller('DemoController', ['$scope', 'author', 'version', function ($scope, author, version) {
$scope.author = author;
$scope.version =version;
}])
</script>
</body>
</html>
9.1 AngularJS 模块加载
<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
<meta charset="UTF-8">
<title>AngularJS 模块加载</title>
</head>
<body>
<div ng-controller="DemoController">
<h1>{{now}}</h1>
<h2>{{str|capitalize}}</h2>
</div>
<script src="./libs/angular.min.js"></script>
<script>
var App = angular.module('App', []);
// 配置$log服务(禁用debug)
// config
// 允许一次配置多个服务(块)
// 传递的一个数组(依赖注入方式)
App.config(['$logProvider', '$filterProvider', function ($logProvider, $filterProvider) {
// $log.debug();
// 禁用debug功能
$logProvider.debugEnabled(false);
// 默认9个过滤器,通过配置可以新增一些过滤器
$filterProvider.register('capitalize', function () {
// 新增一个过滤器
return function (input) {
return input[0].toUpperCase() + input.slice(1);
}
});
}]);
// App.config(['$filterProvider', function($filterProvider) {
// // 默认9个过滤器,通过配置可以新增一些过滤器
// $filterProvider.register('capitalize', function () {
// return function (input) {
// return input[0].toUpperCase() + input.slice(1);
// }
// });
// }])
App.controller('DemoController', ['$scope', '$log', function ($scope, $log) {
// 测试配置后的结果
$log.debug('debug');
$scope.str = 'hello angular';
}]);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
<meta charset="UTF-8">
<title>AngularJS 模块加载</title>
</head>
<body>
<div ng-controller="DemoController">
{{name}}
</div>
<script src="./libs/angular.min.js"></script>
<script>
var App = angular.module('App', []);
// 直接运行$http、$rootScope服务
// $rootScope根作域
App.run(['$http', '$rootScope', function ($http, $rootScope) {
// 直接调用$http
$http({
url: 'example.php',
method: 'get'
});
// 根作用域
$rootScope.name = '祖宗';
}]);
App.controller('DemoController', ['$scope', function($scope) {
//
//
$scope.name = '后代';
}])
</script>
</body>
</html>
10.1 AngularJS 路由和多视图
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AngularJS 路由和多视图</title>
<style>
body {
padding: 0;
margin: 0;
background-color: #F7F7F7;
font-family: Arial;
}
.wrapper {
width: 980px;
margin: 50px auto;
}
ul {
padding: 0;
margin: 0;
overflow: hidden;
list-style: none;
background-color: #000;
border-radius: 4px;
}
li {
float: left;
width: 120px;
height: 40px;
text-align: center;
line-height: 40px;
font-size: 18px;
}
li.active {
background-color: #333;
}
li a {
display: block;
color: #FFF;
text-decoration: none;
}
.content {
margin-top: 30px;
font-size: 24px;
padding: 0 20px;
}
</style>
</head>
<body>
<div class="wrapper">
<!-- 导航菜单 -->
<ul>
<li class="active">
<a href="#index">Index</a>
</li>
<li>
<a href="#introduce">Introduce</a>
</li>
<li>
<a href="#contact">Contact Us</a>
</li>
</ul>
<!-- 内容 -->
<div class="content">
Index Page
</div>
</div>
<script>
// 监听锚点变化然后发送请求
// hashchange事件可以监听锚点变化
window.addEventListener('hashchange', function () {
// console.log(1);
// 获取锚点
var hash = location.hash;
// 处理#
hash = hash.slice(1);
// 实例对象
var xhr = new XMLHttpRequest;
// 将锚点做为参数传递给服务端进处理
xhr.open('get', '10-01.php?hash=' + hash);
xhr.send(null);
xhr.onreadystatechange = function () {
if(xhr.readyState == 4 && xhr.status == 200) {
var result = xhr.responseText;
// 将返回结果添加到页面
document.querySelector('.content').innerHTML = result;
}
}
});
</script>
</body>
</html>
//PHP 本地服务测试代码
<?php
$hash = $_GET['hash'];
switch ($hash) {
case 'index':
echo '<h1>Index Page!</h1>';
break;
case 'introduce':
echo '<h1>Introduce Page!</h1>';
break;
case 'contact':
echo '<h1>Contact Us Page!</h1>';
break;
default:
# code...
break;
}
?>
10.2 AngularJS 路由和多视图
<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
<meta charset="UTF-8">
<title>AngularJS 路由和多视图</title>
<style>
body {
padding: 0;
margin: 0;
background-color: #F7F7F7;
font-family: Arial;
}
.wrapper {
max-width: 980px;
margin: 50px auto;
}
ul {
padding: 0;
margin: 0;
overflow: hidden;
list-style: none;
background-color: #000;
border-radius: 4px;
}
li {
float: left;
width: 120px;
height: 40px;
text-align: center;
line-height: 40px;
font-size: 18px;
}
li.active {
background-color: #333;
}
li a {
display: block;
color: #FFF;
text-decoration: none;
}
.content {
margin-top: 30px;
font-size: 24px;
padding: 0 20px;
}
</style>
</head>
<body>
<div class="wrapper">
<!-- 导航菜单 -->
<ul>
<li class="active">
<a href="#/index">Index</a>
</li>
<li>
<a href="#/introduce">Introduce</a>
</li>
<li>
<a href="#/contact">Contact Us</a>
</li>
<li>
<a href="#/list">List</a>
</li>
</ul>
<!-- 内容 -->
<div class="content">
<!-- 占位符 -->
<div ng-view></div>
</div>
</div>
<!-- AngularJS核心框架 -->
<script src="./libs/angular.min.js"></script>
<!-- 路由模块理解成插件 -->
<script src="./libs/angular-route.js"></script>
<script>
// 依赖ngRoute模块
var App = angular.module('App', ['ngRoute']);
// 需要对路由模块进行配置,使其正常工作
App.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/index', {
// template: '<h1>index Pages!</h1>',
templateUrl: './abc.html'
})
.when('/introduce', {
template: '<h1>introduce Pages!</h1>'
})
.when('/contact', {
// template: '<h1>contact US Pages!</h1>',
templateUrl: './contact.html',
controller: 'ContactController' // 定义控制器
})
.when('/list', {
templateUrl: './list.html', // 视图模板
controller: 'ListController' // 定义控制器
})
.otherwise({
redirectTo: '/index'
});
}]);
// 列表控制器
App.controller('ListController', ['$scope', '$http', function ($scope, $http) {
// 模型数据
// $scope.items = ['html', 'css', 'js'];
$http({
url: '10-02.php',
}).success(function (info) {
// console.log(info);
$scope.items = info;
});
}]);
App.controller('ContactController', ['$scope', '$http', function ($scope, $http) {
$http({
url: 'contact.php'
}).success(function (info) {
$scope.content = info;
});
}]);
</script>
</body>
</html>
//php本地服务测试代码
<?php
// 数组
$items = array('html', 'css', 'js');
echo json_encode($items);
10.3 AngularJS 路由和多视图
<!DOCTYPE html>
<html lang="en" ng-app="App">
<head>
<meta charset="UTF-8">
<title>AngularJS 路由和多视图</title>
<style>
body {
padding: 0;
margin: 0;
background-color: #F7F7F7;
font-family: Arial;
}
.wrapper {
max-width: 980px;
margin: 50px auto;
}
ul {
padding: 0;
margin: 0;
overflow: hidden;
list-style: none;
background-color: #000;
border-radius: 4px;
}
li {
float: left;
width: 120px;
height: 40px;
text-align: center;
line-height: 40px;
font-size: 18px;
}
li.active {
background-color: #333;
}
li a {
display: block;
color: #FFF;
text-decoration: none;
}
.content {
margin-top: 30px;
font-size: 24px;
padding: 0 20px;
}
</style>
</head>
<body>
<div class="wrapper">
<!-- 导航菜单 -->
<ul>
<li class="active">
<a href="#/index/5/abc/7">Index</a>
</li>
<li>
<a href="#/introduce">Introduce</a>
</li>
<li>
<a href="#/contact">Contact Us</a>
</li>
<li>
<a href="#/list">List</a>
</li>
</ul>
<!-- 内容 -->
<div class="content">
<!-- 占位符 -->
<div ng-view></div>
</div>
</div>
<!-- AngularJS核心框架 -->
<script src="./libs/angular.min.js"></script>
<!-- 路由模块理解成插件 -->
<script src="./libs/angular-route.js"></script>
<script>
// 依赖ngRoute模块
var App = angular.module('App', ['ngRoute']);
// 需要对路由模块进行配置,使其正常工作
App.config(['$routeProvider', function ($routeProvider) {
$routeProvider.when('/index/:id/:page/:p', {
templateUrl: 'abc.html',
controller: 'IndexController'
})
.otherwise({
redirectTo: '/index'
});
}]);
// 提供了一个专门负责获取参数的一个服务$routeParams
App.controller('IndexController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {
$scope.content = '练习路由功能';
console.log($routeParams);
}]);
</script>
</body>
</html>
11.1 AngularJS jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>AngularJS jQuery</title>
</head>
<body>
<div ng-controller="DemoController">
<div class="box">普通一个盒子</div>
<button>点击</button>
</div>
<script src="./libs/jquery.min.js"></script>
<script src="./libs/angular.min.js"></script>
<script>
// angular.element() 方法可以将一个原生DOM对象转成jquery对象
// 原生DOM对象
var box = document.querySelector('.box');
var btn = document.querySelector('button');
// 转成jQuery对象
box = angular.element(box);
btn = angular.element(btn);
btn.on('click', function () {
//
// box.css('color', 'red');
box.animate({
fontSize: '40px'
}, 400);
});
// 但是angularJS 只是实现了jquery对象部分方法
</script>
</body>
</html>