Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Use javascript var in AngularJS constant

I'm would like to use a var from a plain old javascript file in an AngularJS constant. Is that possible. Below is what I have tried.

Javascript file:

"use strict";

var CONST_MAP = {
  'key': 'value'
};

AngularJS file:

'use strict';

angular.module('foo').constant('bar', {
    'test': CONST_MAP
});
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It's possible using angular providers.

var CONST_MAP = {
  'key': 'value'
};
var app = angular.module('myApp', []);
app.constant('bar', {
  'test': CONST_MAP
})
app.controller('myCtrl', function ($scope, bar) {
  console.log(bar)
  $scope.name = bar.test.key;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  {{ name }}
</div>

about 4 years ago · Juan Pablo Isaza Report

0

I think that you can do it like this:

angular.module('foo').constant('bar', (function(){
    var CONST_MAP = {
      'key': 'value'
    };
    return {
       test: CONST_MAP
    }
}));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!