Nagularjs Web services

Service service

You can think of service service as a global variable that app provides to user Y

And the purpose of the service

Think of it as an extension to the functionality of each controller

Description:

(1) in the ng framework, service is a noun which is replaced by [ng global variable].

(2) In the NG framework, the concept of service is put forward because the scope of controller cannot communicate with each other due to the rating problem. Different controllers can interact with each other through service injection

(3) There are many different services in the NG framework, including system services and custom services.

Note: There is no superior or inferior service, whether it is a system service or a custom service

System services

$rootscope.

He is an object. $rootScope.erw. Puts the data into the global rootscop

It is necessary but limited

System services $HTTP

The NG framework directly builds a service for handling network functions into the APP data model, that is, HTTP can be regarded as the service provided by the system is prefixed with $

In order to distinguish between services, we do not write the $prefix in our custom services

Syntax: Because the $HTTP service is provided directly by the system, there is no need to define the service content manually. Instead, it is injected directly. Added to the

You can use it directly in controller

App,controller(‘ controller ‘, [‘ scope,scope,scope, HTTP ‘,function(scope,scope, HTTP){

//get

$http({

Method:get ,

Url :”

}).success(function (res){

Console.log(res)

}).error(funcrion(err){ })

}])

//post

$http({

Method:’post”,

Url:’ ‘,

Headers:{

// This is used to express how to send a message

“Content-type” : ‘Application/X-www-from-urlencoded’

},

Data:’username=framk&password=123456

}).success(function (dara){ Console.log(data)})

Note: Injecting the $HTTP service into the Controller only indicates that the controller’s script can use network requests, not that it already uses network requests

The specific network request still needs us to edit manually

Imgsrc and the href of the a tag are copied directly to SRC and href for images like those requested by the Angular framework. You should write the ng-src directive and the n g-href directive

Angularjs filter service $filter

Description: the $filrer service in the NG framework is a small tool to transform data display

Using this service can make the content displayed on the page appear in a different structure

Grammar: {{| any content filter}}

System services

A total of four filters are provided in the Filter service.

Currency. Currency format

Filter subset array. An array used to filter out a subset of an array of options is similar to the input hint function.

Uppercase Lowercase Lowercase letters.

Orderby: expression. Use to sort the display by the value of the expression. Commonly used with ng-repeat

User-defined filtering service

Description The app page data model can be directly used in the page without additional injection operations by extending the.filter() method

Syntax: app.filter(” custom filter service name “, function () {

Return function (text){.return text Function code

}

})

Timeline service (timer)

Description There are three types of timeline services that have no uniform name and must be injected into the Controller before they can be used

Type Interval Service invocation interval Delay Service invocation interval Delay service invocation interval Delay service invocation timeout Clear the timeline content service

There is no difference between supplementing the above three services and using the equal function at the interval point of BO M

grammar

var timer=$interval(function (){

Clear the timeline service is the kind of timer that should be $timer to. cancer

$interval.cancer(timer)

}, 1000).

Listening service $watch

Description; $watch although we call it a service, actually this service does not need our active injection

Instead, they can be used directly from anywhere in the controller at any time

Its role is to listen for any desired value of the n g variable and call back if the value changes

Syntax: scope.scope.scope.swatch (‘ Want to listen for Angular expression or string ‘callback)

Custom service

Description. Services in AngularJ s are essentially extensions to the APP data model in a sense that services are global variables

Custom services, which follow, carry out this idea well

System services are directly provided with different functions

But both require injection before they can be used (special services do not require injection, filterFilter filterSwitch

Note: There are five ways to customize a service: (1) value service (2) constant service. (3) Factory service (4) Service service (5) Provider service

Added. The purpose of a custom service is basically the same: to provide a global variable for direct communication between controllers

Value User-defined service

Description. Value service is a service customization method that extends app data model through value () method. By extending APP, services can exchange information between controllers and act as global variables

Syntax. App.value (‘ Custom service name ‘, ‘JSON for service content’)

app.value(abc,{ Pane:’frank’})

Function (scope,’ ABC ‘,function (scope,’ ABC ‘,function (scope,’ ABC ‘,function (scope, ABC){

}])

Note: The value service is actually a global variable, so its contents can be read and modified at will

Constant User-defined service

Description. The constant service can be considered to be functionally equivalent to the value service

The difference is that once the Content service has been customized, it cannot be changed until injection is used (multiple customizations of the Constant service only take effect first).

Syntax. App. constant (JSON for “Custom service Name” service content)

Factory service

Description: Yes through. The factory () method creates an object structure and returns the operation directly.

Instructions. Returns an object at first glance. So all of the structure of this object is done inside the function and inside the function you can manipulate and extend the object as you want

But you can’t do that in a value fixed structure

App.factory (” custom name “function () {app.factory(” custom name “) function () {

var obj={}

Obj.name=mame

Function(){ }

return obj

})

Servise service

Description: When a blank object has been built with itself, all content operations within the service go through this. To complete the

Syntax. App.servise (‘ custom name ‘, function () {

This.pname=npname

If the service is private. You need a closure to return it out

Var secret =’ This is a little secret ‘

Get secret method

this.getsecret=function(){

return secret

}

Modify secret method

this.setsecret=function(val){

secret=val

}

})

The provider service

Description. The Provider service passes. This.$get=function(){provider ();} } extends the service content to the fixed structure

Special: Once the Provider service is created, it is directly added to the APP

The Provider service always exists whether or not the Controller is injected

None of the other four custom services can do this. The app. Provider (‘ custom service name ‘function () {

this.$get=function(){

var obj={

ob j.name=name

}

return obj

}

})

The following call is directly a custom service name. Name