user2879704
user2879704

Reputation:

how to import json string constants from outside module to angularjs controllers?

my $http post body is too large and i need to make six different $http calls with different post content. i can't keep the body of the post messages (which is nothing but a valid json string) in my controller.js as it bloats it and becomes ugly. is there anything like require in javascript (or) import statements in python that i can use to put all constants in one module and import it into my app.js?

lib.js contents::::::::

var a = 'hello~' module.exports = a

app.js::::::

a = require('lib')
app.controller('aController', function($scope){
console.log(a) //this must print hello~
}

Upvotes: 0

Views: 329

Answers (2)

Dazuku
Dazuku

Reputation: 273

I don't know if I understood well, but the solutions it's to add the 'lib.js' to the HTML before you add the app.js. Like this:

<script src="js/lib.js"></script>
<script src="js/app.js"></script>

And it's not necessary to make the export, you 'lib.js' may look like that:

var a="hello";

And in your app.js put console.log(a)

Upvotes: 1

TommyMac
TommyMac

Reputation: 299

You can create a module that holds constants and is included in your js build process and use $provide.constant('constantName', constantObj); to provide the value. then you can just import the module and then the provider in your controller by doing:

app.module('this.aController', ['constantsModule']);
app.controller('aController', [
  'constantName',
  '$scope',
  function ($scope) {
    console.log(a);
  } 
]);

Upvotes: 0

Related Questions