0

I was wondering what the best moment is while initializing the app to retrieve data from: 1 - a REST service 2 - $routeParams to define application wide constant.

config phase only accepts providers and during the config / run phase $routeParams properties are undefined.

This seems like somewhat in the right direction: Can you pass parameters to an AngularJS controller on creation?

Also: how to define a constant within a controller, is that possible?

app.controller('MainCtrl', function($scope) { 
   //define app.constant here
}

--edit: typo

Community
  • 1
  • 1
San Jay Falcon
  • 993
  • 1
  • 9
  • 20
  • Run runs after the config before the controllers. Run blocks - get executed after the injector is created and are used to kickstart the application. – Shawn C. Sep 29 '14 at 13:47

2 Answers2

1

During run phase all the providers should be initialized and working correctly, you can use the $http service to retrieve what ever parameters are needed for you.

I am pretty sure the $routeParams are initialized at run phase as well.

Defining constants in a controller isn't a good practice (in my opinion), if they are unique to that controller then they are just variables, and if you want real application wide constants, use a service, that's what they are for :)

I know of one easy way to pass parameters to controllers on creation, which is using the angular ui router project: https://github.com/angular-ui/ui-router

In the resolve function you can do http calls if necessary, inject constants etc, it's very handy and personally I never build an angular project without it.

I am pretty sure there are more ways, but usually the best practice to pass data between controllers is using a service.

On a side note, if you have a piece of data that is common to more than 1 controller, the easiest way is to put that data on a service and do a watch on that service return value, for example, say I have isLoggedIn, which can change at any moment and a lot of controllers will want to be notified about it, use a UserService and watch for it's value:

UserService.isLoggedIn = function() {
    return _isLoggedIn;
}

And in your Controller:

$scope.$watch(function() {
    return UserService.isLoggedIn();
}, doSomeAction);

I hope this helps!

PiniH
  • 1,901
  • 13
  • 20
1

This http://www.jvandemo.com/how-to-resolve-angularjs-resources-with-ui-router/ seems like a nice guide, basically, you add the resolve to the state:

.state("customers", {
        url : "/customers",
        templateUrl: 'customers.html',
        resolve: {
            //any value you want, this function should return a promise, 
            //only when that promise is resolved, it will instantiate the controller
            //Make sure however you add some signal that something is happening because
            //while fetching it can seem like the page is not responding
            customers: ['$http', 'anyOtherServiceYouMightNeed', function($http, otherService){
                //return a promise
                return $http.get('api/customers');
            }],
            //and for constant
            constants: ['ConfigService', function(config) {
                 return config.appConstants;
            }]
        },
        //customersCtrl will have customers resolved already
        controller : 'customersCtrl'
      });

      app.controller('customersCtrl', ['$scope', 'customers', 'constants', 
            function($scope, customers, consts) {
            //customers will be ready and resolved when the controller is instantiated
            //you can do this with anything you might need inside a controller
      }
PiniH
  • 1,901
  • 13
  • 20