当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > AngularJS入门教程之服务(Service)

AngularJS入门教程之服务(Service)

2018年05月29日  | 移动技术网IT编程  | 我要评论

angularjs 服务(service)

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

什么是服务?

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

angularjs 内建了30 多个服务。

有个 $location 服务,它可以返回当前页面的 url 地址。

实例

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="myapp" ng-controller="myctrl">
<p> 当前页面的url:</p>
<h3>{{myurl}}</h3>
</div>

<p>该实例使用了内建的 $location 服务获取当前页面的 url。</p>

<script>
var app = angular.module('myapp', []);
app.controller('myctrl', function($scope, $location) {
  $scope.myurl = $location.absurl();
});
</script>

</body>
</html>

运行结果:

当前页面的url:

http://www.runoob.com/try/try.php?filename=try_ng_services

该实例使用了内建的 $location 服务获取当前页面的 url。

注意: $location 服务是作为一个参数传递到 controller 中。如果要使用它,需要在 controller 中定义。

为什么使用服务?

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

angularjs 会一直监控应用,处理事件变化, angularjs 使用 $location 服务比使用 window.location 对象更好。

$http 服务

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

实例

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

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="myapp" ng-controller="myctrl"> 

<p>欢迎信息:</p>

<>{{mywelcome}}<>

</div>

<p> $http 服务向服务器请求信息,返回的值放入变量 "mywelcome" 中。</p>

<script>
var app = angular.module('myapp', []);
app.controller('myctrl', function($scope, $http) {
 $http.get("welcome.htm").then(function (response) {
   $scope.mywelcome = response.data;
 });
});
</script>

运行结果:

欢迎信息:

      欢迎访问

$http 服务向服务器请求信息,返回的值放入变量 "mywelcome" 中。

以上是一个非常简单的 $http 服务实例,更多 $http 服务应用请查看 angularjs http 教程。

$timeout 服务

angularjs $timeout 服务对应了 js window.settimeout 函数。

实例

两秒后显示信息:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="myapp" ng-controller="myctrl"> 

<p>两秒后显示信息:</p>

<h1>{{myheader}}</h1>

</div>

<p>$timeout 访问在规定的毫秒数后执行指定函数。</p>

<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>

</body>
</html>

运行结果:

两秒后显示信息:

how are you today?

$timeout 访问在规定的毫秒数后执行指定函数。

$interval 服务

angularjs $interval 服务对应了 js window.setinterval 函数。

实例

每两秒显示信息:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="myapp" ng-controller="myctrl"> 

<p>现在时间是:</p>

<h1>{{thetime}}</h1>

</div>

<p>$interval 访问在指定的周期(以毫秒计)来调用函数或计算表达式。</p>

<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>

</body>
</html>

运行效果:

现在时间是:

下午3:41:09

$interval 访问在指定的周期(以毫秒计)来调用函数或计

创建自定义服务

你可以创建自定义的访问,链接到你的模块中:

创建名为hexafy 的访问:

app.service('hexafy', function() {
  this.myfunc = function (x) {
    return x.tostring(16);
  }
});

要使用自定义的访问,需要在定义过滤器的时候独立添加:

实例

使用自定义的的服务 hexafy 将一个数字转换为16进制数:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="myapp" ng-controller="myctrl">

<p>255 的16进制是:</p>

<h1>{{hex}}</h1>

</div>

<p>自定义服务,用于转换16进制数:</p>

<script>
var app = angular.module('myapp', []);

app.service('hexafy', function() {
	this.myfunc = function (x) {
    return x.tostring(16);
  }
});
app.controller('myctrl', function($scope, hexafy) {
 $scope.hex = hexafy.myfunc(255);
});
</script>

</body>
</html>

运行结果:

255 的16 进制是:

f f

自定义服务,用于转换16进制数:

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

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

在过滤器 myformat 中使用服务 hexafy:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="myapp">
在过滤器中使用服务:

<h1>{{255 | myformat}}</h1>

</div>

<script>
var app = angular.module('myapp', []);
app.service('hexafy', function() {
	this.myfunc = function (x) {
    return x.tostring(16);
  }
});
app.filter('myformat',['hexafy', function(hexafy) {
  return function(x) {
    return hexafy.myfunc(x);
  };
}]);

</script>

</body>
</html>

运行效果:

在过滤器中使用服务:

         f  f

在对象数组中获取值时你可以使用过滤器:

创建服务 hexafy:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>

<div ng-app="myapp" ng-controller="myctrl">
<p>在获取数组 [255, 251, 200] 值时使用过滤器:</p>

<ul>
 <li ng-repeat="x in counts">{{x | myformat}}</li>
</ul>

<p>过滤器使用服务将10进制转换为16进制。</p>
</div>

<script>
var app = angular.module('myapp', []);
app.service('hexafy', function() {
	this.myfunc = function (x) {
    return x.tostring(16);
  }
});
app.filter('myformat',['hexafy', function(hexafy) {
  return function(x) {
    return hexafy.myfunc(x);
  };
}]);
app.controller('myctrl', function($scope) {
  $scope.counts = [255, 251, 200];
});
</script>

</body>
</html>

运行效果:

在获取数组[255, 251, 200]值时使用过滤器:

  • ff
  • fb
  • c8

过滤器使用服务将10进制转换为16进制。

以上就是对angularjs 服务的资料整理,后续继续补充,有需要的朋友参考下。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网