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
});
CodePudding user response:
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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
I think that you can do it like this:
angular.module('foo').constant('bar', (function(){
var CONST_MAP = {
'key': 'value'
};
return {
test: CONST_MAP
}
}));