Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define constant in one file and access in another file in angularjs

I am handling many variable in my application, I dont want to define the string each and every time. Instead of that I want to access the string from one file and access through all the files in my application.

Eg:

one = 1;

I want to access 1 with the variable name one in my application, because in future it can be changed as one (one = one) that time I cant change in all the place.

In order to avoid such difficulty am going for the constant file.

like image 335
VG__ Avatar asked May 12 '16 06:05

VG__


1 Answers

You can create a service and access it in any controller by injecting it.

angular.module('app')
.constant('CONSTANTS', {something: 12});

your controller

angular.module('app').controller('mainCtrl', function (CONSTANTS) {              
    $scope.mainctrlvariable = CONSTANTS.something;
});

your another controller

angular.module('app').controller('otherCtrl', function (CONSTANTS) {              
        $scope.otherCtrlvariable = CONSTANTS.something;
    });
like image 106
Pravesh Khatri Avatar answered Oct 23 '22 04:10

Pravesh Khatri