AngularJS dependency injection details and sample code, angularjs sample code

Source: Internet
Author: User

AngularJS dependency injection details and sample code, angularjs sample code

Dependency injection is a software design pattern given in the component that replaces the hard components to encode their dependencies. This reduces the dependency and dependency configuration of a component. This helps make components reusable, maintained, and tested.

AngularJS provides a supreme dependency injection mechanism. It provides a core component that can be injected and dependent on each other.

Value

Factory

Service

Provider

Constant Value

Value

A value is a simple JavaScript Object used to configure the Phase Controller during value transfer.

//define a modulevar mainApp = angular.module("mainApp", []);//create a value object as "defaultInput" and pass it a data.mainApp.value("defaultInput", 5);...//inject the value in the controller using its name "defaultInput"mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {   $scope.number = defaultInput;   $scope.result = CalcService.square($scope.number);   $scope.square = function() {   $scope.result = CalcService.square($scope.number);  }});

Factory

The factory is the value used to return the function. It creates values based on requirements whenever a service or controller needs them. It usually uses a factory function to calculate and return the corresponding value.

//define a modulevar mainApp = angular.module("mainApp", []);//create a factory "MathService" which provides a method multiply to return multiplication of two numbersmainApp.factory('MathService', function() {     var factory = {};   factory.multiply = function(a, b) {   return a * b   }  return factory;}); //inject the factory "MathService" in a service to utilize the multiply method of factory.mainApp.service('CalcService', function(MathService){   this.square = function(a) {    return MathService.multiply(a,a);   }});...

Service

A service is a single JavaScript that contains a set of function objects to execute certain tasks. The service uses the service () function and injects it into the definition of the controller.

//define a modulevar mainApp = angular.module("mainApp", []);...//create a service which defines a method square to return square of a number.mainApp.service('CalcService', function(MathService){   this.square = function(a) {    return MathService.multiply(a,a);   }});//inject the service "CalcService" into the controllermainApp.controller('CalcController', function($scope, CalcService, defaultInput) {   $scope.number = defaultInput;   $scope.result = CalcService.square($scope.number);   $scope.square = function() {   $scope.result = CalcService.square($scope.number);  }});

Provider

Services and factories in the configuration phase during the internal creation of AngularJS used by the provider (phase during which AngularJS directs itself ). The script mentioned below can be used for creation. We have already created MathService. A provider is a special factory method and get () method used to return values, services, and factories.

//define a modulevar mainApp = angular.module("mainApp", []);...//create a service using provider which defines a method square to return square of a number.mainApp.config(function($provide) {  $provide.provider('MathService', function() {   this.$get = function() {     var factory = {};      factory.multiply = function(a, b) {      return a * b;      }     return factory;   };  });});

Constant

Constants are used to consider facts by configuring the phase value. values cannot be transferred during the configuration phase.

MainApp. constant ("configParam", "constant value ");

Example

The following example shows all the preceding commands.

TestAngularJS.html

Result

Open textangularjs.html in the webbrowser. The result is as follows.

The above is the compilation of AngularJS dependency injection materials. We will continue to add relevant materials in the future. Thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.