In an attempt to manually bootstrap AngularJS following data retrieval from the server, I am facing a dilemma.
Here is my implementation in the bootstrap.js
file:
var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');
var maindata;
function bootstrapAngular() {
angular.element(document).ready(function() {
angular.bootstrap(document, ['app']);
});
}
function getMainData() {
return maindata;
}
$http
.get('/data')
.then(function (response) {
maindata = response.data;
bootstrapAngular();
}, function (error) {
console.log(error);
});
The challenge lies in the need to access the maindata
or call getMaindata()
within the app after bootstrapping. This requires not enclosing the Javascript code within an immediately invoked function expression ((function(){})();
) in the bootstrap.js
file, to allow visibility of the function and variable to other parts of the app.
Is there a way to ensure privacy while still enabling access from other sections of the app?