Confused about Service vs Factory

后端 未结 20 2452
轻奢々
轻奢々 2020-11-22 08:49

As I understand it, when inside a factory I return an object that gets injected into a controller. When inside a service I am dealing with the object using this

20条回答
  •  -上瘾入骨i
    2020-11-22 09:37

    Service style: (probably the simplest one) returns the actual function: Useful for sharing utility functions that are useful to invoke by simply appending () to the injected function reference.

    A service in AngularJS is a singleton JavaScript object which contains a set of functions

    var myModule = angular.module("myModule", []);
    
    myModule.value  ("myValue"  , "12345");
    
    function MyService(myValue) {
        this.doIt = function() {
            console.log("done: " + myValue;
        }
    }
    
    myModule.service("myService", MyService);
    myModule.controller("MyController", function($scope, myService) {
    
        myService.doIt();
    
    });
    

    Factory style: (more involved but more sophisticated) returns the function's return value: instantiate an object like new Object() in java.

    Factory is a function that creates values. When a service, controller etc. needs a value injected from a factory, the factory creates the value on demand. Once created, the value is reused for all services, controllers etc. which need it injected.

    var myModule = angular.module("myModule", []);
    
    myModule.value("numberValue", 999);
    
    myModule.factory("myFactory", function(numberValue) {
        return "a value: " + numberValue;
    })  
    myModule.controller("MyController", function($scope, myFactory) {
    
        console.log(myFactory);
    
    });
    

    Provider style: (full blown, configurable version) returns the output of the function's $get function: Configurable.

    Providers in AngularJS is the most flexible form of factory you can create. You register a provider with a module just like you do with a service or factory, except you use the provider() function instead.

    var myModule = angular.module("myModule", []);
    
    myModule.provider("mySecondService", function() {
        var provider = {};
        var config   = { configParam : "default" };
    
        provider.doConfig = function(configParam) {
            config.configParam = configParam;
        }
    
        provider.$get = function() {
            var service = {};
    
            service.doService = function() {
                console.log("mySecondService: " + config.configParam);
            }
    
            return service;
        }
    
        return provider;
    });
    
    myModule.config( function( mySecondServiceProvider ) {
        mySecondServiceProvider.doConfig("new config param");
    });
    
    myModule.controller("MyController", function($scope, mySecondService) {
    
        $scope.whenButtonClicked = function() {
            mySecondService.doIt();
        }
    
    });
    

    src jenkov

    
        
        
        	
        	
        	JS Bin
        
        
        	{{serviceOutput}}
        	

    {{factoryOutput}}

    {{providerOutput}}

    jsbin

    
    
    
    	
    	
    	JS Bin
    
    
    
    {{hellos}}

    jsfiddle

提交回复
热议问题