How to Injected to Angular constant?

In AngularJs, we are using global constant and value. Both are working same but only difference in constant and value as give below.

·         The constants value can't change anywhere in across application that means the value of constants can't inject.
·         But the value can be change anywhere in across application as per your requirements that means the value of constants can be inject anywhere in application.


In the below example, I am going to share the how to Inject the constant objects and use of constant objects. 

Please see the detail as given below.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Injected to Angular constant</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.min.js"></script>
    <script>
        var app = angular.module('ConstApp', []);

        //CREATE A CONSTANT OBJECT AS "CONSTANT" AND PASS IT TO CONTROLLER.
        app.constant("constant", 'constant value.');

        app.controller('constCtrl', ["$scope", "constant", function ($scope, constant) {
            $scope.name = constant;
        }]);
    </script>
</head>
<body ng-app="ConstApp" ng-controller="constCtrl">
    <h1>Injected to Angular constant</h1>
    <p>Result : {{name}}</p>
</body>
</html>

About Blogger

ANIL SINGH

Hey! I am Anil Singh. I am Active Blogger, Writer and Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
http://www.code-sample.com
http://www.code-sample.xyz

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book