0

I am connecting to an API through a script (init.js) when starting my angular app. I then receive som information such as a username and a user ID that I want to define as global values to use in the AngularJS App.

How do I pass the variables on to use in the AngularJS app?

My thought now is to define the variables in the MainController to be able to use them anywhere.

init.js

(function () {
function appStart() {
    //Get variables
    var userId = 123;
    var username = 'This is my name';

    //Init Angular App
    angular.bootstrap(document, ['myApp']); //How do I use the variables in the angular app?
}

function genericError() {
    console.error('Something went wrong');
}

TT.native.init()
.done(appStart)
.fail(genericError);
})();

app.js

(function () { //Start
var app = angular.module('myApp', [
'myControllers',
'myDirectives',
'myFilters',
'myServices',

'ui.router'
]);

controller.js

var app = angular.module('myControllers', []);

app.controller('MainController', function ($scope, $state) {
    $scope.userName = ""; //I want to use the variable from INIT.JS here
    $scope.userId = 0; //I want to use the variable from INIT.JS here
});
3
  • Why don't you connect the api within angular app? Commented Oct 22, 2014 at 9:33
  • How would I do that? I don't know how to. I am using this system based on jQuery that needs to have the init code look like that. Commented Oct 22, 2014 at 9:36
  • You can build a service for your first code block (to get the user data). Then you can call it in a run block within angularjs (app.run(...)). After that point whenever you need these variables, you can call them from that service. I know it might be too much for a beginner in angular, but that's a consistent and "angular" way to do it... Commented Oct 22, 2014 at 9:41

3 Answers 3

1

The easiest solution would be:

var userId = -1;
var username = '';
(function () {
function appStart() {
    //Get variables
    userId = 123;
    username = 'This is my name';

    //Init Angular App
    angular.bootstrap(document, ['myApp']); //How do I use the variables in the angular app?
}

function genericError() {
    console.error('Something went wrong');
}

TT.native.init()
.done(appStart)
.fail(genericError);
})();

Then in the controller:

app.controller('MainController', function ($scope, $state) {
    $scope.userName = userId;
    $scope.userId = username;
});

But just know that this is not a good approach. You should connect to your api within the angular app. Maybe you should watch and read some tutorials before beginning to code...

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! In what way is it not a good approach? I would very much like to see how I could do it within the Angular app. before the first state is loaded. If I could perform the same thing there as right now in init.js, that would of course be great!
Start digging services (factory method) and run method of angularjs. And accept this as correct answer if it's a solution for the moment...
okay, would be helpful though to know in what way it is a bad approach so I can analyze if it will be a lasting solution.
0

You can use angular.module('myApp').value or angular.module('myApp').constant before bootstrapping angular. In your controller you should add variables into dependencies.

5 Comments

Thanks! Sounds like a good approach. Could you please provide an example? I am not an experienced coder.
Okay, so I can add the values. But how do I set the variables to the .value when I bootstrap angular?
angular.module('myApp').value('userName', userName)
I've figured out that part, but not how to get the userName value there from the init.js when I run: angular.bootstrap(document, ['myApp']);
app.controller('MainController',['$scope', '$state', 'userName', function ($scope, $state, userName) { $scope.userName = userId; $scope.userId = username; }]);
0

You probably using ngRouter or uiRouter so you could resolve the TT.native.init before entering the first route/state.

1 Comment

Yes, using Ui router. How would I do that? Could you put it into code?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.