On Mon, Aug 18, 2014 at 2:53 PM, Jonathan Matthew Beck <[email protected]>
wrote:

> I came into a project and found things like this in the routing:
>
>                 .when('/home', {
>
>                     templateUrl: 'partials/home.html',
>
>                     controller: 'HomeController as vm',
>
>                     resolve: {
>
>                         init: ['dataService', function (dataService) {
>
>                             return dataService.init();
>
>                         }]
>
>                     }
>
>                 })
>
> I looked for documentation on the use of resolve like that.  As far as I
> can tell, it's creating an object and then passing, or overriding, the
> objects init function.  Then it's creating an un-named Angular service (?)
> and passing in the dataService.
>
> Questions:
>
> 1) What is the above code doing?
>
It is defining a route. The resolve part basically says "Don't render this
route until all this data is available." The most common thing that resolve
functions do is retrieve data from REST services. Maybe dataService.init
does that. If the functions in a resolve return promises, Angular waits for
all of those be resolved before it renders the view for that route.

> 2) Does it create a new instance of an un-named something (service?) every
> time the user is routed?
>
It creates a new instance of HomeController every time the user goes to
that route.

> 3) The square brackets are confusing me, are we over-writing the
> Controller’s init() function every time?
>

The square brackets are just creating a literal JavaScript array that
contains a string and a function. It is one way to make an injectable
function save for minimizing. That won't mean much if you are just getting
started with AngularJS, but later it will make sense.

-- 
R. Mark Volkmann
Object Computing, Inc.

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to