0

I am trying to re-write an application using angular.js, but I still do not see how it works (more or less).

For instance. In my previous code, I had a function that was executed once everything was loaded that initialised variables, accessed for everyone in a window.variable style.

Now I want the same here.

My idea was to build a factory to return an object with all the variables, and then make that everyone had access to this object (somehow).

The questions are: 1- Am I right? I should initialise variables through a factory? 2- And how can I "call" this factory-method at the beginning of the code? with the module.run function?

Cheers,

3 Answers 3

1

I'd probably put the variables in a service, and then inject that into a wrapping angular controller. Any other controllers that you would want to have access to these 'global' variables would be nested under the wrapping controller, thus inheriting the variables.

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

app.service("globVars", function () {
  var vars = {};
  vars.someVar = "a";
  vars.someOtherVar = "b";
  return vars;
});

app.controller("WrappingCtrl", function ($scope, globVars) {
  $scope.globVars = globVars;
});

app.controller("NestedCtrl", function ($scope) {
  console.log($scope.globVars.someVar); // => "a"
});


<html ng-app="app">
  <div id="wrapper" ng-controller="WrappingCtrl">
    <div class="nested" ng-controller="NestedCtrl">
      {{globVars.someVar}}
    </div>
  </div>
</html>
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, that's exactly what I was thinking and it works (using a factory instead of a service though). Thanks a lot!
wrapping controller is not a good idea... instead I would rather inject the service only in the places where you need to use the variables... not every part of your application necessarily need to know about those variables
That doesn't sound bad at all. Would it be too much if I ask for some simple example code of how to inject the service??
*You mean like in the WrappingCtrl, put there the service in any controller that I need. I'll work on it :)
I agree with you Doodec, partially. In some cases, it surely would be a better idea to inject the variable service into the controllers that actually are to use it. Another scenario might be that you have a set of variables for a particular part of your view layer. In this case, It'd make sense to use an inherited set of variables from a wrapping controller. If, all of the nested controllers are to have access to it. Otherwise, you're not looking for global (or in this case, wrapped) variables.
1

I think you should avoid using global variables as much as you could

But if you have to use them, you are right, you can initialize them in module.run and add the module in the app dependencies

Comments

1

If you want to access some variables, you must see to $rootScope, because you can access $rootScope everywhere.

Also you must use angular config and run functions.

I advice to you don't use global scope, because it affects on app speed, memory leaks etc.

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.