<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>serviceAndDefinitService</title>

</head>
<body>
  <!--1:-->
  <div ng-app="myModule" ng-controller="ctrl">
    {{title}}<button ng-click="changeTitle()">change title</button>
    <button id="btn">change by jQuery</button>
  </div>

  <!--2:-->
  <div ng-app="module2" ng-controller="Ctrl2">
    {{name}}
    {{num}}
  </div>

  <!--4:-->
   <div ng-app="module3" ng-controller="Ctrl3">
    <div ng-bind="name"></div>
    <div ng-bind-html="name"></div>
  </div>

  <!--5:-->
   <div ng-app="module5">
    <div ng-controller="Ctrl5"></div>
  </div>

  <!--6:-->

  <table ng-app="module6" ng-controller="Ctrl6" border="1" width="400px" ng-cloak>
    <tr>
      <th>name</th>
      <th>age</th>
    </tr>
    <tr ng-repeat="v in data">
      <td>{{v.name}}</td>
      <td>{{v.age}}</td>
    </tr>
  </table>
  <script src="http://cdn.bootcss.com/angular.js/1.4.6/angular.js"></script>
  <script src="https://cdn.bootcss.com/jquery/1.8.3/jquery.js"></script>
  <script type="text/javascript">
    /*Angular.js之服务与自定义服务(服务:可以在多个应用场景下复用的代码片段)
    1:$scope.$apply()//在使用非$scope方法时需要调用angular.js的底层$apply()方法使$scope数据自动更新到view;
    2:$timeout与$interval服务,$timeout.cancel()取消延时器服务
    3:$window服务
    4:$sce服务安全的处理html数据,$trustAsHtml,view中需要加ng-bind-html属性;
    5:$cacheFactory缓存服务
    6:$http服务获取后台数据/$http服务缓存操作/$http服务简写操作/$http服务后台接收post数据
    7:使用factory自定义服务/多个控制器共享服务数据的处理
    8:使用service创建自定义服务*/

/*1:$scope.$apply()*/
  var m=angular.module('myModule',[]);
  m.controller('ctrl',['$scope',function($scope){
    $scope.title='puDong shop';
    //使用绑定到$scope上的方法更改$scope上的数据会自动更新视图
    $scope.changeTitle=function(){
    $scope.title='www.pudong.net';
  };
  //通过jQuery更改$scope数据时需要调用$apply()方法才能更新视图
    $('#btn').click(function(){
      $scope.title='www.pudong.com';
      $scope.$apply();
    });
 }])

/*2:$timeout与$interval服务*/
  var module2=angular.module('module2',[]);
  module2.controller('Ctrl2', ['$scope','$interval',function ($scope,$interval) {
    $scope.name="puDong store";
    $scope.num=1;
    $interval(function(){
      $scope.num++;
    },1000);
  }])

/*4:$sce服务安全的处理html数据,$trustAsHtml,ng-bind-html;*/
  var module3=angular.module('module3',[]);
  module3.controller('Ctrl3', ['$scope','$sce', function ($scope,$sce) {
    $scope.name=$sce.trustAsHtml('<h1>This is URL of The puDong store !</h1>');
  }])

/*5:$cacheFactory缓存服务*/
  var module5=angular.module('module5',[]);
  module5.controller('Ctrl5', ['$scope','$cacheFactory', function ($scope,$cacheFactory) {
    //创建缓存容器,指定容器名称
    var obj=$cacheFactory('module5Mall');
    //写入缓存数据
    obj.put('title','puDong Store');
    obj.put('data',{'url':'http://www.pudong.com'});
    console.log(obj.get('title'));//puDong Store
    console.log(obj.get('data').url);//http://www.pudong.com
    //删除数据
    obj.remove('title');
    obj.removeAll();
    //销毁容器
    obj.destroy();
  }])

/*6:$http服务获取后台数据*/
  var module6=angular.module('module6',[]);
  module6.controller('Ctrl6', ['$scope','$http', function ($scope,$http) {
    $http({
      method:'get',
      url:'http://localhost/ajax.php',
      cache:true//$http服务缓存是使用$cacheFactory服务,只需要配置参数cache为true即可,当我们异步请求有个页面多次的时候,第一次的请求结果被缓存到页面中,后面的请求就不再发生,直接使用第一次的请求结果,
    }).then(function(response){
      $scope.data=response.data;
    },function(err){console.log(err);})
    //$http服务的简写:$http.get/post('',{}).then(function(){},function(){})第一个参数指定url,第二个参数指定其他配置选项
    $http.get('http://localhost/ajax.php',{
    cache:true
    }).then(function(){},function(){})
  }])

/*7:使用factory自定义服务/service自定义服务:service and factory的区别:factory时普通function,而service是一个构造器(constructor),Angular在调用service时会用new关键字,而调用factory时只是调用普通的function*/
  var module6=angular.module('module6',[]);
  //使用factory自定义服务
  module6.factory('factoryService',['$http',function($http){
    return {
      get:function(){
        return $http({url:'http://localhost/ajax.php'});
      }
    }
  }])
  //使用service自定义服务
  module6.service('factoryService',['$http',function($http){
    this.get=function(){
      return $http({
        method:'GET',
        url:'http://localhost/ajax.php',
      }).then(function(response){return response.data;},function(erro){return erro;})
    }
  }]);
  module6.controller('Ctrl6', ['$scope','factoryService', function ($scope,factoryService) {
    factoryService.get().then(function(response){
      $scope.data=response.data;
    })
  }])
</script>
</body>
</html>

Angular.js之服务与自定义服务学习笔记的更多相关文章

  1. AngularJS内建服务以及自定义服务的用法

    在AngularJS中, 服务是一个比较重要的部分,它是一个对象或者是函数,可以在你的AngularJS的应用中使用.接下来介绍几种比较常用的内建服务以及自定义服务的方法. [内建服务] (1)loc ...

  2. 《Node.js开发实战详解》学习笔记

    <Node.js开发实战详解>学习笔记 ——持续更新中 一.NodeJS设计模式 1 . 单例模式 顾名思义,单例就是保证一个类只有一个实例,实现的方法是,先判断实例是否存在,如果存在则直 ...

  3. Angular.js之自定义指令学习笔记

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. 在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(老罗学习笔记6)

    一:Eclipse下 1.创建工程: ---- 2.创建后目录 3.添加java函数 4.在src下创建package,在package下创建file 5.res---layout下创建xml文件,命 ...

  5. 【Microsoft Azure学习之旅】消息服务Service Bus的学习笔记及Demo示例

    今年项目组做的是Cloud产品,有幸接触到了云计算的知识,也了解并使用了当今流行的云计算平台Amazon AWS与Microsoft Azure.我们的产品最初只部署在AWS平台上,现在产品决定同时支 ...

  6. vue.js 2.0 官方文档学习笔记 —— 01. vue 介绍

    这是我的vue.js 2.0的学习笔记,采取了将官方文档中的代码集中到一个文件的形式.目的是保存下来,方便自己查阅. !官方文档:https://cn.vuejs.org/v2/guide/ 01. ...

  7. 字节跳动内部微服务架构-Docker实战学习笔记分享 真香

    前言 基于 Spring Cloud 的微服务设计和开发,已经越来越多地得到了更多企业的推广和应用,而 Spring Cloud 社区也在不断的迅速发展壮大之中,近几年时间,Spring Cloud ...

  8. 在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务(老罗学习笔记5)

    在数字科技日新月异的今天,软件和硬件的完美结合,造就了智能移动设备的流行.今天大家对iOS和Android系统的趋之若鹜,一定程度上是由于这两个系统上有着丰富多彩的各种应用软件.因此,软件和硬件的关系 ...

  9. MVC2 扩展Models和自定义验证(学习笔记)

    当我们利用Visual Studio生成实体类以后,难免会用到验证功能(例如,用户登录时验证用户名是否为空,并加以显示). Visual Studio实体类:实体类 如果直接去编辑Visual Stu ...

随机推荐

  1. UITableView 之 取消选中

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [table ...

  2. PHP常用函数(收集)

    <?php //===============================时间日期=============================== //y返回年最后两位,Y年四位数,m月份数字 ...

  3. Angular - - $templateCache 和 $templateRequest

    $templateCache 第一次使用模板,它被加载到模板缓存中,以便快速检索.你可以直接将模板标签加载到缓存中,或者通过$templateCache服务. 通过script标签: <scri ...

  4. CentOS 7 安装 JDK

    1. 卸载旧版 1.1. 查看版本信息 java -version 1.2. 查看JDK信息 rpm -qa | grep java 1.3. 卸载 rpm -e --nodeps tzdata-ja ...

  5. Intent的属性及Intent-filter配置——Component属性

    Intent的Component属性需要接受一个ComponentName对象,ComponentName对象包含如下几个构造器. ComponentName(String pkg,String cl ...

  6. 日历视图(CalendarView)组件的功能和用法

    日历视图(CalendarView)可用于显示和选择日期,用户既可选择一个日期,也可通过触摸来滚动日历.如果希望监控该组件的日历改变,可调用CalendarView的setOnDateChangeLi ...

  7. jQuery Deferred和Promise的使用介绍:

    deferred对象是从jquery1.5.0引入的一个新对象,ES6也引入了Promise的正式规范. 抽象来说,deferreds 可以理解为表示需要长时间才能完成的耗时操作的一种方式,相比于阻塞 ...

  8. 关于angularjs过滤器的小尝试

    最近的项目中用到了angularjs,相比传统的jquery直接操作Dom, 开发web项目,angularjs在操作表格数据时的数据绑定,操作让我不禁直呼过瘾,好方便啊, 从后台接口传一个json过 ...

  9. Redis系列二(yum切换为网易163)

    这个可能和Redis没有直接的关系... 是我在yum install的时候发现centos的yum实在是太慢,上网查了下.网易163有个yum镜像,为了让CentOS6使用速度更快的YUM更新源,可 ...

  10. 抽象类 abstract 和 接口 interface 类的区别

    在看一些框架的优秀改良时,都会设计一层base层,并且 base里面一般都是 abstract 类,然后 就找了为什么做的原因.发现: PHP5支持抽象类和抽象方法.抽象类不能直接被实例化,你必须先继 ...