二、Angular服务

1.什么是服务

中你可以创建自己的服务,或使用内建服务。

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

AngularJS 内建了30 多个服务。

服务分为 内建服务 和 自定义服务

2.常用的内建服务

2.1 $location 服务 (≈window.location)

服务对象的表层内容

/**
   * 
   * cc {$$protocol: "file", $$host: "", $$port: null, $$parse: ƒ, $$compose: ƒ, …}
   * $$absUrl: "file:///E:/wxpx/lianxi/Angular/20211021/03/1/angular$location.html#/?username=50&pass=25",
   * $$compose: ƒ ()arguments: (...)caller: (...)length: 0name: ""prototype: {constructor: ƒ}[[FunctionLocation]]: angular.js:11565[[Prototype]]: ƒ ()[[Scopes]]: Scopes[4]
   * $$hash: "",
   * $$host: "",
   * $$parse: ƒ (d)arguments: (...)caller: (...)length: 1name: ""prototype: {constructor: ƒ}[[FunctionLocation]]: angular.js:11491[[Prototype]]: ƒ ()[[Scopes]]: Scopes[4]
   * $$parseLinkUrl: ƒ (a,c),
   * $$path: "/",
   * $$port: null$$protocol: "file",
   * $$replace: false,
   * $$search: pass: "25"username: "50",
   * [[Prototype]]: Object$$state: null$$url: "/?username=50&pass=25"[[Prototype]]: Object
   * 
   * 
   * */

 代码例

<script>
  let modelobj = angular.module("mymodul",[]);
  modelobj.controller("myctrol",function($scope,$location){
    $scope.showURL = function(){
      console.log($location)
       //返回当前的url
      $scope.urladdress = $location.absUrl();
    }
  })
</script>

2.2 $http 服务(封装ajax)

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

代码例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>$http</title>
  <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
</head>
<body ng-app="app" ng-controller="myctrl">
  <button ng-click="getStudentInfo()">获取西安的天气信息</button>
  <table border="1px">
    <tr><th>时间</th><th>天气</th><th>温度</th><th>风向</th><th>风级</th></tr>
    <tr ng-repeat="element in weather">
      <td>{{element.date}}</td>
      <td>{{element.cond.txt_d}}</td>
      <td>{{element.tmp.min}}</td>
      <td>{{element.wind.dir}}</td>
      <td>{{element.wind.sc}}</td></tr>
  </table>
</body>
<script>
  var mymodul = angular.module("app",[]);
  mymodul.controller("myctrl",function($scope,$http){
    $scope.getStudentInfo = function(){
      $scope.weather=null;
       $http.get("https://free-api.heweather.com/v5/weather?city=西安&key=d7bd43af19c64994b62fc643e5d75272")
       .then((res)=>{
         console.log(res.data.HeWeather5[0].daily_forecast)
        $scope.weather  = res.data.HeWeather5[0].daily_forecast;
       })
    }
  })
</script>
</html>

$http请求后返回的是一个 普通的对象

 

2.3 $timeout (JS window.setTimeout )

<script>

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope, $timeout) {

  $scope.myHeader = "Hello World!";

  $timeout(function () {

      $scope.myHeader = "How are you today?";

  }, 2000);

});

</script>

2.4 $interval JS window.setInterval 

<script>

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope, $interval) {

  $scope.theTime = new Date().toLocaleTimeString();

  $interval(function () {

      $scope.theTime = new Date().toLocaleTimeString();

  }, 1000);

});

</script>

3.自定义服务

创建流程

1. 引入angular.js文件

2. 在script 文件或者标记中 通过angular.module 创建模型对象

3. 通过 模型对象.serivce("自定义服务名",fuction(){});

        function  需要执行一系列的操作

        function函数中的 this.属性/对象  会将属性/对象绑定在 自定义服务对象中

4. 在 模型对象的函数中  以参数的形式引入 自定义服务

代码例

var app = angular.module("app",[]);
    app.service('derfind',function($http){
      this.mystart = function(x){
        return "x = " + x;
    }
})
app.controller("mytrol",function($scope,derfind){
  $scope.df = derfind.mystart(25);
})

无奈源于不够强大

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值