<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Service</title>
</head>
<body ng-app="app" ng-controller="myCtrl"> <input type="text" ng-model="cont" >
<hr>
<button ng-click="get()">Get</button> <ul ng-show="user">
<li>{{user.id}}</li>
<li>{{user.name}}</li>
<li>{{user.age}}</li>
</ul> <script src="bower_components/angular/angular.js"></script> <script> var app = angular.module("app",[]); // app.controller("myCtrl",["$scope","$http",function ($scope,$http) {
// $scope.get=function () {
// $http({
// method:'GET',
// url:'data.json'
// }).then(function suc(response) {
// $scope.user=response.data;
// console.log(response);
// });
// }
// }]) // app.controller("myCtrl",["$scope","$timeout","$http",function ($scope,$timeout,$http) {
// var timer;
// $scope.$watch("cont",function (newCont) {
// if(newCont) {
// if(timer){
// $timeout.cancel(timer);//延时500毫秒后请求数据,避免频繁的请求
// }
// timer = $timeout(function () {
// $http({
// method: 'GET',
// url: 'data.json'
// }).then(function suc(response) {
// $scope.user = response.data;
// console.log(response);
// });
// },500);//延时500毫秒后请求数据,避免频繁的请求
//
// }
// })
// }]) //自定义服务
app.factory("GetService",["$http",function ($http) {
return {
doget:function (url) {
return $http({
method: 'GET',
url: url
});
}
}
}]);
//自定义服务,放在最后一个参数中
app.controller("myCtrl",["$scope","$timeout","$http","GetService",function ($scope,$timeout,$http,GetService) {
var timer;
$scope.$watch("cont",function (newCont) {
if(newCont) {
if(timer){
$timeout.cancel(timer);//延时500毫秒后请求数据,避免频繁的请求
}
timer = $timeout(function () {
//使用自定义服务
GetService.doget('data.json').then(function suc(response) {
$scope.user = response.data;
console.log(response);
});
},500);//延时500毫秒后请求数据,避免频繁的请求
}
})
}]) </script> </body>
</html>

Angular 服务的简单使用的更多相关文章

  1. AngularJS中$http服务的简单用法

    我们可以使用内置的$http服务直接同外部进行通信.$http服务只是简单的封装了浏览器原生的XMLHttpRequest对象. 1.链式调用 $http服务是只能接受一个参数的函数,这个参数是一个对 ...

  2. AngularJS中的http服务的简单用法

    我们可以使用内置的$http服务直接同外部进行通信.$http服务只是简单的封装了浏览器原生的XMLHttpRequest对象. 1.链式调用 $http服务是只能接受一个参数的函数,这个参数是一个对 ...

  3. angular服务二

    angular服务 $http 实现客户端与服务器端异步请求 get方式 test.html <!DOCTYPE html> <html lang="en"> ...

  4. angular服务一

    angular服务 [$apply()] jquery内数据绑定 要用$apply(),不然不能在js内通过angular绑定数据 <!DOCTYPE html> <html lan ...

  5. 分布式系统的消息&服务模式简单总结

    分布式系统的消息&服务模式简单总结 在一个分布式系统中,有各种消息的处理,有各种服务模式,有同步异步,有高并发问题甚至应对高并发问题的Actor编程模型,本文尝试对这些问题做一个简单思考和总结 ...

  6. angular 服务 service factory provider constant value

    angular服务 服务是对公共代码的抽象,由于依赖注入的要求,服务都是单例的,这样我们才能到处注入它们,而不用去管理它们的生命周期. angular的服务有以下几种类型: 常量(Constant): ...

  7. springboot微服务的简单小结

    springboot微服务的简单小结 近来公司用springboot微服务,所以小结一下. 基础: 什么是SpingBoot微服务? 如何创建SpringBoot微服务? 如何管理和完善SpringB ...

  8. .net平台 基于 XMPP协议的即时消息服务端简单实现

    .net平台 基于 XMPP协议的即时消息服务端简单实现 昨天抽空学习了一下XMPP,在网上找了好久,中文的资料太少了所以做这个简单的例子,今天才完成.公司也正在准备开发基于XMPP协议的即时通讯工具 ...

  9. PHP Swoole-Demo TCP服务端简单实现

    tcp 服务端简单demo与client . <?php /** * author : rookiejin <mrjnamei@gmail.com> * createTime : 2 ...

随机推荐

  1. 【胡思乱想】命令模式 与 Thread Runnable

    实现线程任务有两种方式,一种是创建Runnable/Callable对象,传递给Thread.另一种是子类重写Thread的run方法. 我觉得前一种像极了命令模式,或者说应该是命令模式的一个使用案例 ...

  2. css3 2D转换(2D Transform) 动画(Animation)

    transform 版本:CSS3 内核类型 写法 Webkit(Chrome/Safari) -webkit-transform Gecko(Firefox) -moz-transform Pres ...

  3. linux-openvpn

    1.安装openvpn 1)安装需要的依赖,需要用到epel源 #yum -y install  epel-release 修改epel.repo文件enabled=1 #yum install ea ...

  4. ng-app与ng-app='myApp'的区别

    ng-app与ng-app=""是一样的,都是没定义应用名的,例如 <div ng-app="" ng-init="firstName='ech ...

  5. 使用vertical-align实现垂直对齐

    关于垂直对齐,之前研究过好几次了,但感觉每次都没研究透彻,做了几个效果,就觉得自己掌握了,实在是自欺欺人.真乃搞技术的大忌. 这两天又下定决心重新开始研究vertical-allign这个高深莫测的属 ...

  6. JavaScript回调函数及数组方法测试

    JavaScript回调函数及数组方法测试 具体代码如下: <!DOCTYPE html> <html lang="en"> <head> &l ...

  7. NYOJ 1013 除法表达式(欧几里德算法+唯一分解定理)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1013 描述 给出一个这样的除法表达式:X1/X2/X3/···/Xk,其中Xi是 ...

  8. 【SpringBoot系列4】SpringBoot定制自己的bean

    起因:SpringBoot我是越用越喜欢,但当SpringBoot出了问题的时候,我却无从下手,因为封装实在是太高度化了.然后之前有一个需求,使用SpringBoot提供的StringRedisTem ...

  9. 并发编程——ConcurrentHashMap#transfer() 扩容逐行分析

    前言 ConcurrentHashMap 是并发中的重中之重,也是最常用的数据结果,之前的文章中,我们介绍了 putVal 方法.并发编程之 ConcurrentHashMap(JDK 1.8) pu ...

  10. C# 金额转中文大写

    今天看到一个库是把金额转中文大写,看起来很容易,所以我就自己写了 创建的项目是创建一个 dot net core 的项目,实际上这个项目可以创建为 Stand 的. 首先创建类,这个类的构造传入一个 ...