This article mainly introduces you to the details about the services of angularjs, as well as the usage of angularjs built-in services. It contains usage examples of angularjs. Let’s take a look at this article together.
The essence of the service is a singleton object that provides data and objects.
Two major categories:
##scope
window
##timeout etc.
Using built-in services Methods provided:
The first step is to inject the services you need into function(
location)
The second step is to call the method data provided in the service. .
Case:
<!DOCTYPE html><html ng-app="myApp"><head lang="en"> <meta charset="UTF-8"> <title></title> <script src="js/angular.js"></script></head><body><p ng-controller="myCtrl"> <img ng-src="img/{{imgList[index]}}" alt=""/> <h1>{{count}}</h1></p><script> var app = angular.module('myApp', ['ng']); app.controller('myCtrl', function ($scope,$interval) { $scope.count = 0; timer = $interval(function () { $scope.count++; if($scope.count > 20) { $interval.cancel(timer) } },500) $scope.index = 0; $scope.imgList = ['1.jpg','2.jpg','3.jpg'] $interval(function () { $scope.index++; if($scope.index > 2) { $scope.index = 0; } },1000) });</script></body></html>
Methods to customize services: app.factory ('Service name', function(){//Normal method return {}})
app.service('Service name', function(){//Constructor})
app.constant('Service name', {})//Create a constant service
app.value(' Service name', {})//Create variable service
<!DOCTYPE html><html ng-app="myApp"><head lang="en">
<meta charset="UTF-8">
<title></title>
<script src="js/angular.js"></script></head><body><p ng-controller="myCtrl">
<button ng-click="handleClick()">clickMe</button></p><script>
var app = angular.module('myApp', ['ng']); //创建自定义服务
app.factory('$output', function () {
return {
print: function () {
console.log('func in $output is called');
}
}
})
app.controller('myCtrl', function ($scope,$output) {
$scope.handleClick = function () {
//调用自定义服务所提供的方法
$output.print();
}
});</script></body></html>
<script>
var app = angular.module('myApp', ['ng']); //自定义服务
app.service('$student', function () {
this.study = function () {
console.log('we are learning...');
} this.name = "zhangSan";
})
app.controller('myCtrl', function ($scope, $student) {
$student.study();
});</script></body></html>
app.value('服务名称',{})//创建变量服务**<script> var app = angular.module('myApp', ['ng']); //创建常量服务 app.constant('$Author',{name:'KKK',email:'**@163.com'}) //创建变量服务 app.value('$Config',{version:1}) app.controller('myCtrl', function ($scope,$Author,$Config) { console.log($Author.email); console.log($Config.version); }); </script>
Notes:
Customized services need to use methods in other services<body><p ng-controller="myCtrl">
<button ng-click="start()">开始</button>
<button ng-click="stop()">结束</button></p><script>
var app = angular.module('myApp', ['ng']); //通过service方法去创建自定义服务
app.service('$HeartBeat', function ($interval) {
this.startBeat = function () {
promise = $interval(function () {
console.log('beat...');
},1000);
} this.stopBeat = function () {
$interval.cancel(promise);
}
})
app.controller('myCtrl', function ($scope,$HeartBeat) {
$scope.start = function () {
$HeartBeat.startBeat();
}
$scope.stop = function () {
$HeartBeat.stopBeat();
}
});</script>
to learn). If you have any questions, you can leave a message below.
The above is the detailed content of How to use angularjs services? Specific introduction to the use of angularjs services. For more information, please follow other related articles on the PHP Chinese website!