The Service in AngularJS

AngularJS supports the concepts of "Seperation of Concerns" using the service structure. Service is JavaScript functions and is responsible for certain tasks. It makes them separate entities that are easy to maintain and test. Controller, filter can call them simply. Service is often injected using AngularJS's dependency injection mechanism.

AngularJS provides a lot of predefined services: $ http, $ scope, $ route, $ window, $ location . Each service has certain tasks. For example, $ http is used to create ajax requests on the server to retrieve data. $ route is used to define routing information . The default AngularJS service starts with the $ symbol.

There are two ways to create a service:

  1. factory
  2. service

Use factory method in AngularJS

When using the factory method, we first define the factory and attach the method to it.

 var ungdungAngularJS = angular . module ( "ungdungAngularJS" , []); ungdungAngularJS . factory ( 'toanhocService' , function () { var factory = {}; factory . phepnhan = function ( a , b ) { return a * b } return factory ; }); 

Use service method in AngularJS

Using the service method, we will define the service then assign the method to it. We also inject the services available to it.

 ungdungAngularJS . service ( 'tinhBPService' , function ( toanhocService ) { this . binhphuong = function ( a ) { return toanhocService . phepnhan ( a , a ); } }); 

For example:

Below is an example to illustrate the above instruction.

testAngularJS.jsp

 Vi du Service in AngularJS  src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js" > 

 

 Cavalier AngularJS  ng-app = "ungdungAngularJS" ng-controller = "tinhBPController" > 
 So much:  type = "number" ng-model = "number" />  ng-click = "binhphuong ()" > X 2 
 Pass through: {{ketqua}} 

Result:

Open the textAngularJS.jsp page on the web browser and see the results:

The Service in AngularJS Picture 1

Follow tutorialspoint

Last post: Scope in AngularJS

Next lesson: Multilingual (i18n) in AngularJS

4 ★ | 1 Vote

May be interested

  • Multilingual (i18n) in AngularJSMultilingual (i18n) in AngularJS
    angularjs provides multi-lingual functions (i18n) with 3 types of filters, currency, date and number. we just need to combine the js corresponding to the location depending on the country. by default it will attach to the location in the web browser.
  • Expression in AngularJSExpression in AngularJS
    expression is used to bind application data to html tags. expression is written in {{expression}}. expression has a similar way of operating as the ng-bind directive.
  • Controller component in AngularJSController component in AngularJS
    an angularjs application works primarily based on the controller component to control the flow of data in the application.
  • MVC structure in AngularJSMVC structure in AngularJS
    model view controller or mvc is a popular call, a software design model for web-based applications.
  • Create the table in AngularJSCreate the table in AngularJS
    table data is often repeated in natural order. in angularjs, ng-repeat directive can be used to draw tables easily. below is an example of how to use ng-repeat to create tables.
  • Scope in AngularJSScope in AngularJS
    scope is a special javascript object with the role of linking controllers and views. scope contains information as model data. in the controller, model data can be accessed via the $ scope object.
  • Include syntax in AngularJSInclude syntax in AngularJS
    html does not support embedding html web pages in html pages. to achieve this function, the following methods can be used:
  • How to check which service Viettel is using on the phoneHow to check which service Viettel is using on the phone
    the use of too many services in the phone will sometimes cause your phone money to be deducted for unknown reasons, even though there is no use of that service. therefore, it is necessary to check what subscriber is using the service.
  • SkyDrive online storage service marks 250 million usersSkyDrive online storage service marks 250 million users
    according to the latest report from microsoft giant, their skydrive storage service has just reached 250 million users. it is worth noting that this service has increased by 50 million subscribers since windows 8 was released 7 months ago.
  • 12 things to know when choosing a Web Hosting service12 things to know when choosing a Web Hosting service
    choosing an internet / web hosting service provider (web hosting service) in accordance with the requirements of the business is a seemingly simple task but not simple. this article takes a look at some of the essentials in choosing hosting service providers that are suitable for your business.