Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Registering Service using value in angularJS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.12k
    Comment on it

    A service is created by a service factory and service factories are functions which are created by a service provider(constructor function).

    When instantiating service provider, there must be a property named $get, which holds service factory logic.

    The $provide provide helping method for registering services with the help of $injector.



    Method Name Type Instantiable Description

    provider

    Provider

    Yes

    This method registers a service provider

    constant

    object

    No

    Registers a constant service(eg. string, number, array, object or function), accessible to providers and services both.

    value

    object

    No

    Registers a value service(eg. string, number, array, object or function), accessible to services, not providers.

    factory

    function

    Yes

    A factory injects a plain function so AngularJS will call the function in controller. Factory returns the service instance and its provider contain a $get property that holds the factory function.

    service

    class

    No

    A service is an injectable constructor. You can specify the dependency in controller where we want to access service data.



    When we talk about registering a service using value, here value is nothing more than a simple injectable value. The value can be a string, number, array, function, object. The difference between value and constant is, it can not be injected into configuration. It accepts two parameters, that are: name and value. Syntax is:


    value(name, value);
    

    where name is any string and value can be any string, number, object or function.

    Here I am explaining a short example of implementing value as a service.
    JS file:


    var app= angular.module(app,[]);
    app.value(username,Alex);
    app.controller(userCtrl, function ($scope,username) {
        $scope.uname = username;
    });
    

    Now, we can get username in our html file:


    <body ng-app=app>
        <div ng-controller=userCtrl>
            {{uname}}
        </div>
    </body>
    

    For more information you can visit:


    https://docs.angularjs.org/api/auto/service/$provide

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: