0

I have a userService that deals with all user functionality. In order to populate it with data my _Layout.cshtml creates a global currentUser variable which I then want to put into my userService. How can I do this during bootstrap?

It feels like I should be able to do something like this:

var myApp = angular.module('myApp', [...]);
var userService = myApp.create("userService");
userService.setUser(currentUser);
Cœur
  • 34,719
  • 24
  • 185
  • 251
Chris
  • 25,677
  • 44
  • 189
  • 332

1 Answers1

1

You can use run to perform this before the controllers are used. Something along the lines of:

myApp.run(['userService', function(userService){
  userService.setUser(currentUser); // Consider injecting currentUser, too...
}]);

If it needs to be done earlier than the run events are triggered (though, generally, for this sort of stuff run should suffice), you'll probably want to put it in a config function (myApp.config(/*etc.*/);).

However, those cannot access typical Service providers, so you'll have to do it a bit more custom: https://stackoverflow.com/a/21650337/624590

Community
  • 1
  • 1
DRobinson
  • 4,391
  • 20
  • 31