0

I am trying to manually bootstrap AngularJS after receiving data from the server.

bootstrap.js:

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);
    });

I need to use the maindata or getMaindata() in the app after it is bootstrapped. Therefore, I cannot have javascript closer (function(){})(); on bootstrap.js file to keep everything private, so that the function & variable is accessible to the other part of the app.

Is it possible to make everything private but still accessible to the other part of the app?

3
  • So the question is that, you want to execute a certain block of code, that will fetch some data from the server, and use those values in your app, Before anything else happens ?? Commented May 20, 2015 at 3:27
  • @TechMa9iac Yes, that is what I am trying to do. I guess New Dev's answer solve my problem. Or, you have better suggestion how to write it? Commented May 20, 2015 at 7:29
  • Nope. I guess @NewDev's response fits the requirement (y) Commented May 20, 2015 at 9:08

1 Answer 1

1

You could expose maindata as an injectable to make it available in your app:

$http
    .get('/data')
    .then(function (response) {
        maindata = response.data;

        angular.module("app").value("MainData", maindata);

        bootstrapAngular();
    }, function (error) {
        console.log(error);
    });

Then you could inject it in controllers or services:

.controller("MainCtrl", function($scope, MainData){
   $scope.data = MainData;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.