<!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. jemalloc总结

    jemalloc支持SMP系统和并发多线程,多线程的支持是依赖于多个'arenas',并且一个线程第一次调用内存mallocer,与其相关联的是一个特殊的arena. 线程分配arena只有三种可能的 ...

  2. JDBC Oracle sys 用户连接

    Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection( &quo ...

  3. SQL分区表示例

    -- Create tablecreate table TT_FVP_OCR_ADDRESS( id NUMBER not null, waybill_no VARCHAR2(32) not null ...

  4. RSA实现前端数据加密

    一.前言 一般在登录注册的时候,不能以明文的方式传递数据到后台,如果是http下,很容易被劫持.所以对数据进行加密是常规做法. 二.RSA算法 ”RSA加密算法是一种非对称加密算法.对极大整数做因数分 ...

  5. 大数据技术之_08_Hive学习_05_Hive实战之谷粒影音(ETL+TopN)+常见错误及解决方案

    第10章 Hive实战之谷粒影音10.1 需求描述10.2 项目10.2.1 数据结构10.2.2 ETL原始数据10.3 准备工作10.3.1 创建表10.3.2 导入ETL后的数据到原始表10.3 ...

  6. POJ 2234 Matches Game (尼姆博弈)

    题目链接: https://cn.vjudge.net/problem/POJ-2234 题目描述: Here is a simple game. In this game, there are se ...

  7. Mac Hadoop的安装与配置

    这里介绍Hadoop在mac下的安装与配置. 安装及配置Hadoop 首先安装Hadoop $ brew install Hadoop 配置ssh免密码登录 用dsa密钥认证来生成一对公钥和私钥: $ ...

  8. C++11 使用 std::async创建异步程序

    c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...

  9. hadoop的checkpoint检查时间参数设置

    1.通常情况下,SecondaryNameNode 每隔一小时执行一次. 在hdfs-default.xml文件中: <property> <name>dfs.namenode ...

  10. HDU1045(KB10-A 二分图最大匹配)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...