constants in AngularJs

Constants in AngularJs

Hello everyone, I am going to share the code sample for "how to create the constants in AngularJs?" and "how to inject in angular controller?". The defined constants are ready to use by angular controller as give below.

/**
 * @angular root module.
 */
var app = angular.module('myConstantApp');

/**
 * @The constants defined in myServiceConst are ready for use by controller.
 */
app.constant('myServiceConst',
           { blankMsg: "Please enter your service’s number." ,
             numLessGraterThen10 :'put msg'});

/**
  * Controller using constants.
  * @ng-Inject : my  myServiceConst  is going to inject by the linkToMyServices contoller.
 */
app.controller('linkToMyServices', function ($scope, myServiceConst) {
  
    /**
     * @Initialize the default variable.
    */
    $scope.isError = false;


    /**
       * @name Check Service
       * @desc This scope is used to validate the services for link to new service.
       * @param {mobile no / service no}
       * @returns {validate msg}
   */
    $scope.CheckService = function (mobile) {
        $scope.isError = true;

        //If services number is blank then
        if (mobile == null) {
            alert(myServiceConst.blankMsg);
            $scope.lblMessages = myServiceConst.blankMsg;
        }
    };
});
The HTML code
<div class="confirmServiceFirst">
    <input ng-model="txtCheckServiceNo" type="text">
    <a ng-click="CheckService(txtCheckServiceNo)">Check service</a>
    <a ng-show="lnkChangeService">Change service</a>
    <label ng-model="lblMessages" ng-show="isError">{{lblMessages}}</label>
</div>

The output look like below images.



ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^