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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Buy Books Online at Best Prices

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.