01-使用provider函数创建服务
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="lib/angular.js"></script>
</head>
<body ng-app="app">
<div ng-controller="MainController">
{{name}}
</div>
<script>
//创建provider分为三个步骤
var app = angular.module('app', [])
//1创建一个函数,并且在函数中返回$get()函数对象
var myFunction = function () {
var name = "张三"
return {
$get: function () {
return {
//这里的函数才返回真正的数据
msg: name
}
},
changName: function (newName) {
name = newName
}
}
}
//2.用模块名provider来创建服务,并在第1个参数中为服务命名
//第2个参数是提供功能的函数
app.provider('myService', myFunction)
//3.用模块名config来添加配置信息,使用服务名+provider,然后就可以调用相关函数
app.config(function (myServiceProvider) {
myServiceProvider.changName('李四')
})
//使用自定义服务,只需要在控制器的function参数里进行注入声明即可
app.controller('MainController', function ($scope, myService) {
$scope.name = myService.msg
})
</script>
</body>
</html>
02-使用factory函数创建服务
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="lib/angular.js"></script>
</head>
<body ng-app="app">
<div ng-controller="MainController">
{{name}}
</div>
<script>
var app=angular.module('app',[])
//创建factory服务,并在服务的参数中定义服务名,和服务器提供的功能函数
app.factory('myService',function(){
return{
name:'张三',
changeName:function(newName){
this.name=newName
}
}
})
app.controller('MainController',function($scope,myService){
myService.changeName('李四')
$scope.name=myService.name
})
</script>
</body>
</html>
03-service函数创建服务
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="lib/angular.js"></script>
</head>
<body ng-app="app">
<div ng-controller="MainController">
<input type="text" ng-model="msg">
<input type="button" value="按钮" ng-click="say()">
</div>
<script>
var app=angular.module('app',[])
//创建service服务,service服务必须创建一个构造函数,构造函数中定义对象或功能函数
var myFunction=function(){
this.msg='Hello world'
this.say=function(){
alert(this.msg)
}
}
app.service('MyService',myFunction)
app.controller('MainController',function($scope,MyService){
$scope.msg=MyService.msg
$scope.say=MyService.say
})
</script>
</body>
</html>
04-$location
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="lib/angular.js"></script>
</head>
<body ng-app="app">
<div ng-controller="MainController">
<p>$location.absUrl信息:完整的url地址</p>
<p>{{absUrl}}</p>
<hr>
<p>$location.host信息:url主机信息</p>
<p>{{host}}</p>
<hr>
<p>$location.port信息:端口号</p>
<p>{{port}}</p>
<hr>
<p>$location.hash信息:hash地址</p>
<p>{{hash}}</p>
<hr>
<p>$location.path信息:哈希地址中的子地址</p>
<p>{{path}}</p>
<hr>
</div>
<script>
var app = angular.module('app', [])
// 使用$location,需要在控制器的参数中进行注入声明
app.controller('MainController', function($scope, $location){
$scope.absUrl = $location.absUrl()
$scope.host = $location.host()
$scope.port = $location.port()
// 在url的后面添加hash值‘123’
$scope.hash = $location.hash(123).hash()
// 在url的后面添加子路径
$scope.path = $location.path('/about').path()
})
</script>
</body>
</html>
05-$watch监听$location
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="lib/angular.js"></script>
</head>
<body>
<div ng-app="app">
<div ng-controller="MainController">
<a href="#!/all">全部</a>
<a href="#!/invite">邀请中</a>
<a href="#!/accept">已接受</a>
<a href="#!/refuse">已拒绝</a>
</div>
</div>
<script>
var app=angular.module('app',[])
//对$location 进行注入声明
app.controller('MainController',function($scope,$location){
//将$loaction挂载在$scope中,目的是为了使用$watch的监听
$scope.location=$location
//$watch监听$location的path()
$scope.$watch('location.path()',function(newVal,ordVal){
console.log(newVal)
})
})
</script>
</body>
</html>
06-$routeParams
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="lib/angular.js"></script>
<!-- 1引入angular-route -->
<script src="lib/angular-route.js"></script>
</head>
<body>
<div ng-app="app">
<div ng-controller="MainController">
<a href="#!/index">index</a>
<a href="#!/05">05</a>
<a href="#!/about/2/lucy">lucy</a>
<a href="#!/about/3/Karry">Karry</a>
</div>
<!--3 在HTML标签中绑定ng-view指令,负责子页面视图的渲染 -->
<div ng-view></div>
</div>
<script>
//2在主模块中依赖ngRoute模块
var app = angular.module('app', ['ngRoute'])
app.controller('MainController', function () {
})
//3使用模块名.config来配置路由实现,在function的参数里对$routeProvider服务进行注入声明
app.config(function ($routeProvider) {
//配置路由规则,参数1:待匹配的子路径,参数2是设置子页面视图和控制器的对象
$routeProvider
.when('/index', {
template: '<h2>{{name}}</h2>', //设置视图模板,匹配成功后将渲染到页面的ng-view区域
controller: function ($scope) { //设置控制器,将挂载的数据绑定在视图中
$scope.name = 'index'
}
})
.when('/05', {
templateUrl: './05-$watch监听$location.html'
})
.when('/about/:type/:name', {
template: '<h2>{{type}}:{{name}}</h2>',
controller: function ($scope, $routeParams) { //注入声明,获取路由规则中参数
console.log($routeParams)
$scope.type = $routeParams.type
$scope.name = $routeParams.name
}
})
//没有匹配到规则时,一般设置跳转到主页
.otherwise({
redirectTo:'/index'
})
})
</script>
</body>
</html>
07-使用$http引用json文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="lib/angular.js"></script>
</head>
<body>
<div ng-app="app">
<div ng-controller="MainController">
{{msg}}
</div>
</div>
<script>
var app = angular.module('app', [])
app.controller('MainController', function ($scope, $http) {
//$http发送请求,获取服务端或者本地文件的数据
$http({ //在$http参数里写一个对象,在对象里设置请求的参数
method: 'get', //如果仅读数据就用get,如果需要传参就用post
url: 'data.json', //读取的文件路径,实际开发中一般是服务器端口地址
// params: { 'name': '张三' } //传递参数对象
}).then(function (okResult) { //响应成功的回调函数,数据信息由参数带回
$scope.msg=okResult.data.msg
}), function (errResult) { //响应失败的回调函数,错误信息由参数带回
console.log(errResult.data)
console.log(errResult.status)
}
})
</script>
</body>
</html>
data.json
{
"msg":"hello world"
}
实例案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./lib/angular.js"></script>
<script src="./lib/angular-route.js"></script>
</head>
<body>
<div ng-app="app">
<a href="#!/index">index</a>
<a href="#!/first">05</a>
<a href="#!/about/2/lucy">lucy</a>
<a href="#!/about/3/banny">banny</a>
<div ng-view></div>
</div>
<script>
var app=angular.module('app',['ngRoute'])
app.config(function($routeProvider){
$routeProvider
.when('/index',{
template:'<h2>{{name}}</h2>',
controller:function($scope){
$scope.name='index'
}
})
.when('/first',{
templateUrl:'05-$watch监听$location.html'//跳转05页面
})
.when('/about/:id/:name',{
template:'<h2>{{params}}</h2>',
controller:function($scope,$routeParams){
$scope.params=$routeParams
}
})
.otherwise({
redirectTo:'/index'
})
})
</script>
</body>
</html>
邀请示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./lib/angular/angular.js"></script>
<link rel="stylesheet" href="./lib/bootstrap/bootstrap.css">
<!-- 引入ModelService.js文件,这样才能使用js文件中的模块和其他元素,
而且必须放在angular.js的引入之后-->
<script src="./js/ModulService.js"></script>
<style>
body {
background-color: orange
}
.row {
margin: 15px 0;
/*上下15px 左右0px */
}
</style>
</head>
<body ng-app="app">
<div class="container" ng-controller="MainController">
<!-- 标题栏 -->
<div class="row text-center">
<h1>邀请名单</h1>
</div>
<!-- 嘉宾信息输入框 -->
<div class="row form-inline">
<div class="input-group">
<div class="input-group-addon">姓名</div>
<input type="text" class="form-control" placeholder="请输入姓名" ng-model="info.name">
</div>
<div class="input-group">
<div class="input-group-addon">电话</div>
<input type="text" class="form-control" placeholder="请输入电话" ng-model="info.phone">
</div>
<button type="button" class="btn btn-success" ng-click="invite()">邀请</button>
</div>
<!-- 嘉宾状态筛选按钮组 -->
<div class="row">
<a href="#!/all" class="btn btn-success btn-sm">显示全部</a>
<a href="#!/invite" class="btn btn-success btn-sm">显示邀请中</a>
<a href="#!/accept" class="btn btn-success btn-sm">显示已接受</a>
<a href="#!/refuse" class="btn btn-success btn-sm">显示已拒绝</a>
</div>
<!-- 嘉宾列表 -->
<div class="row">
<table class="table table-hover table-bordered">
<thead>
<tr>
<th>编号</th>
<th>嘉宾姓名</th>
<th>嘉宾电话</th>
<th>嘉宾状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="guest in guests">
<td>{{ $index + 1 }}</td>
<td>{{ guest.name }}</td>
<td>{{ guest.phone }}</td>
<td>{{ guest.state }}</td>
<td>
<!-- 当嘉宾状态为邀请中时才显示接收邀请、拒绝邀请按钮 -->
<span ng-if="guest.state == '邀请中'">
<button type="button" class="btn btn-success btn-xs" ng-click="guest.accept()">接收邀请</button>
<button type="button" class="btn btn-danger btn-xs" ng-click="guest.refuse()">拒绝邀请</button>
</span>
<button type="button" class="btn btn-default btn-xs" ng-click="remove(guest)">删除</button>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
//在主模块中依赖model模块,目的为了使用model模块定义的服务
app = angular.module('app', ['model'])
// 注入声明服务modelService,和$location
app.controller('MainController', function ($scope, $location, modelService) {
var state = Guest.ALL // 默认显示全部嘉宾
// 获取嘉宾列表,并挂载在$scope的属性中
$scope.guests = modelService.getList(state)
$scope.location = $location
// 监听$location中的子路径,对列表进行筛选
$scope.$watch('location.path()', function (newVal) {
switch (newVal) {
case '/all':
state = Guest.ALL
break;
case '/invite':
state = Guest.INVITE
break;
case '/accept':
state = Guest.ACCEPT
break;
case '/refuse':
state = Guest.REFUSE
break;
}
// 重新获取嘉宾列表,更新页面
$scope.guests = modelService.getList(state)
})
// 挂载嘉宾信息,并与页面的输入框进行双向绑定
$scope.info = { name: '', phone: '' }
$scope.invite = function () {
//添加嘉宾信息到列表中
var msg = modelService.add($scope.info.name, $scope.info.phone)
switch (msg.code) {
case 1:
alert('姓名或电话不能为空')
break;
case 2:
alert('嘉宾电话不能重复')
break;
}
}
$scope.remove=function(guest){
modelService.remove(guest)
// 重新获取嘉宾列表,更新页面
$scope.guests = modelService.getList(state)
}
})
</script>
</body>
</html>