• AngularJs Service


    什么是服务?

    在AngularJS中,服务是一个函数或对象,可以在AngularJs应用中使用。

    AngularJs中内建了30多个服务,当然,也可以自定义服务。

    为什么是服务? 

    在很多服务中,比如$location服务,它可以使用DOM中存在的对象,类似window.location对象,但window.location对象在AngularJS应用中有一定的局限性。

      因为这些服务可以获取到AngularJS应用声明周期的每一个阶段,并且和$watch整合,让Angular可以监控应用,处理事件变化。普通的DOM对象则不能在AngularJS应用声明周期中和应用整合。

    $watch:持续监听数据上的变化,更新界面,如: 

      <div ng-app="myApp" ng-controller="myCtrl">
            <label for="">姓:<input type="text" ng-model="firstName"></label><br />
            <label for="">名:<input type="text" ng-model="lastName"></label><br />
            <h1>全名:{{fullName}}</h1>
        </div>
      var app = angular.module("myApp",[]);
      app.controller("myCtrl",function($scope){
          $scope.firstName = "hehe";
          $scope.lastName = "老街";
          //监听firstName的变化,更新fullName
          $scope.$watch("firstName",function(){
              $scope.fullName = $scope.firstName +","+$scope.lastName;
          })
          //监听lastName的变化,更新fullName
          $scope.$watch("lastName",function(){
              $scope.fullName = $scope.firstName +","+$scope.lastName;
          })
      })

    1.$location服务:返回当前页面的URL地址

        <div ng-app="myApp" ng-controller="myCtrl">
            {{myUrl}}
        </div>
      var app = angular.module("myApp",[]);
      app.controller("myCtrl",function($scope,$location){
          $scope.myUrl = $location.absUrl();
      })

    2.$http服务

    $http是AngularJS应用中最常用的服务。服务向服务器发送请求,应用响应服务器传送过来的数据。

    使用$http服务向服务器请求数据:

      var app = angular.module("myApp",[]);
      app.controller("myCtrl",function($scope,$location,$http){
          $scope.myUrl = $location.absUrl();
          $http.get("welcome.html").then(function(response){
              $scope.myWelcome = response.data;
          })
      })

    3.$timeout服务

    AngularJS $timeout服务对应了JS window.setTimeout函数

        <div ng-app="myApp" ng-controller="myCtrl">
            <p>{{ myUrl }}</p>
            <p>{{ myHeader }}</p>
        </div>
        var app = angular.module("myApp",[]);
        app.controller("myCtrl",function($scope,$location,$http,$timeout){
            $scope.myUrl = $location.absUrl();
            // $http.get("welcome.html").then(function(response){
            //     $scope.myWelcome = response.data;
            // })
            $scope.myHeader = "Hello World!";
            $timeout(function(){
                $scope.myHeader = "How are you today?";
            },2000)
        })

    4.$interval服务

    AngularJS中 $interval 服务对应JS中window.setInterval函数

        <div ng-app="myApp" ng-controller="myCtrl">
            <p>{{ theTime }}</p>
        </div>
        var app = angular.module("myApp",[]);
        app.controller("myCtrl",function($scope,$interval){
            $scope.theTime = new Date().toLocaleTimeString();
            $interval(function(){
                $scope.theTime = new Date().toLocaleTimeString();
            },1000)
        })

    创建自定义服务

    创建名为hehe的访问:

        <div ng-app="myApp" ng-controller="myCtrl">
            <p>{{he}}</p>
        </div>
       var app = angular.module("myApp",[]);
        app.controller("myCtrl",function($scope,hehe){
            $scope.he = hehe.myFunc(255);
        })
        app.service("hehe",function(){
            this.myFunc = function (x) {
                return x.toString(16);
            }
        })

    过滤器中,使用自定义服务 

    当你创建了自定义服务,并连接到你的应用上后,你可以在控制器,指令,过滤器或其他服务中使用它。

    在过滤器myForm中使用服务hehe

        var app = angular.module("myApp",[]);
        app.controller("myCtrl",function($scope,hehe){
            $scope.he = hehe.myFunc(255);
        })
        // 自定义服务hehe
        app.service("hehe",function(){
            this.myFunc = function (x) {
                return x.toString(16);
            }
        })
        // 在过滤器myFormat中使用服务hehe
        app.filter("myFormat",["hehe",function(hehe) {
            return function (x) {
                return hehe.myFunc(x);
            }
        }])

    用$apply,实现每一秒显示信息功能(实现$interval功能)

    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.theTime = new Date().toLocaleTimeString();
        $scope.setTime = function() {
            // $apply 方法可以修改数据
            $scope.$apply(function() {
                $scope.theTime = new Date().toLocaleTimeString();
            });
        };
        setInterval($scope.setTime, 1000);
    });
  • 相关阅读:
    VMware虚拟机中常见的问题汇总
    Windows10下安装VMware虚拟机并搭建CentOS系统环境
    myeclipse2017使用总结
    mybatis如何通过接口查找对应的mapper.xml及方法执行详解
    (转)将SVN从一台服务器迁移到另一台服务器(Windows Server VisualSVN Server)
    (转)Maven中的库(repository)详解 ---repository配置查找构件(如.jar)的远程库
    Git知识讲解
    (转)MyEclipse中使用git
    在SpringBoot中添加Logback日志处理
    (转)Spring Boot干货系列:(七)默认日志logback配置解析
  • 原文地址:https://www.cnblogs.com/old-street-hehe/p/6826301.html
Copyright © 2020-2023  润新知