Thanks Кошелев. I've been trying to find something like this and do resolved based on factories/services that depend on $state (UI-Router). This should do the trick!
On Wednesday, November 13, 2013 11:51:43 AM UTC-5, Кошелев Иван wrote: > > Okey, found your topic while tackling the same problem. The solution we > eventually discovered is to use constants for map objects and inject them > during .config(...) via $injector. > > Here is an updated fiddle: > *http://jsfiddle.net/hX7XJ/*<http://jsfiddle.net/hX7XJ/> > > I understand it's been a year, so the answer is for who ever comes here > looking next. > > On Tuesday, October 23, 2012 10:33:26 PM UTC+3, Sebastian Bauer wrote: > >> OK, this SO example is great for understanding. Seeing the official doc >> example (to delay the route switch) it doesn't make sense to try that with >> a service - via alias. An alias is really just to inject services to the >> controller - through the route. Actually I didn't get it by reading the >> docs. >> >> But now it's clear and make sense when and for what to use a service >> alias here. >> >> Thanks to you all, >> Sebastian >> >> Am Dienstag, 23. Oktober 2012 21:00:29 UTC+2 schrieb Pawel Kozlowski: >>> >>> Funny thing, someone had a similar question on SO today: >>> http://stackoverflow.com/q/13037051/1418796 >>> >>> Cheers, >>> Pawel >>> >>> On Tue, Oct 23, 2012 at 8:03 PM, Sebastian Bauer >>> <[email protected]> wrote: >>> > Hi Pete, >>> > >>> > yes, totally right. Maybe my idea was theoretically. But I tried to >>> directly >>> > work with a service. In the docs >>> > (http://docs.angularjs.org/api/ng.$routeProvider): >>> > >>> > factory - {string|function}: If string then it is an alias for a >>> service. >>> > Otherwise if function, then it is injected and the return value is >>> treated >>> > as the dependency. If the result is a promise, it is resolved before >>> its >>> > value is injected into the controller. >>> > >>> > And actually I can put in a service via a string. But it's only called >>> once. >>> > Just wondering if this behaviour is expected and not working for my >>> usecase. >>> > >>> > I put my resolver functions directly into my module config. It works >>> and >>> > still is not in the global scope. >>> > >>> > Am Dienstag, 23. Oktober 2012 19:50:46 UTC+2 schrieb Peter Bacon >>> Darwin: >>> >> >>> >> The DetailsCntlResolver in Pawel's first fiddle is actually an >>> AngularJS >>> >> service! The service is a function that is injected into the resolve >>> >> function and then called. >>> >> Pete >>> >> >>> >> >>> >> On 23 October 2012 18:31, Sebastian Bauer <[email protected]> >>> wrote: >>> >>> >>> >>> Hi Pavel, >>> >>> >>> >>> working with the code examples is definitely easier. ;) >>> >>> >>> >>> Actually, both of your fiddles using plain functions. Not a service >>> as it >>> >>> is theoretically possible. The result for me is: A service isn't the >>> right >>> >>> way for what I need. >>> >>> >>> >>> I just tried to pack all related stuff inside my AngularJS modules. >>> But >>> >>> for these route resolver I'm going to use just simple functions. >>> >>> >>> >>> Thanks a lot for your support. >>> >>> Sebastian >>> >>> >>> >>> Am Dienstag, 23. Oktober 2012 16:23:34 UTC+2 schrieb Pawel >>> Kozlowski: >>> >>>> >>> >>>> Hi Sebastien, >>> >>>> >>> >>>> Yep, sorry, wasn't clear enough and kind of hard to explain things >>> >>>> without the running code (this is why it is always good to send a >>> >>>> jsFiddle / plunk!). >>> >>>> Here is the working jsFiddle with what I had on my mind: >>> >>>> http://jsfiddle.net/Avb4U/1/ >>> >>>> >>> >>>> But as I've mentioned, it might not be worth it if you are not >>> aiming >>> >>>> at reusing the DetailsCntlResolver. So the simple way of doing this >>> is >>> >>>> just: >>> >>>> http://jsfiddle.net/86czx/3/ >>> >>>> >>> >>>> Hope this helps, >>> >>>> Pawel >>> >>>> >>> >>>> On Tue, Oct 23, 2012 at 4:15 PM, Sebastian Bauer >>> >>>> <[email protected]> wrote: >>> >>>> > Hi Pavel, >>> >>>> > >>> >>>> > thank you for the quick response. >>> >>>> > >>> >>>> > Unfortunately it doesn't work. >>> >>>> > >>> >>>> > http://jsfiddle.net/fmcjJ/1/ >>> >>>> > >>> >>>> > In this fiddle I expect that in detail.html view the id is 1 or >>> 2. For >>> >>>> > the >>> >>>> > two detail inks. >>> >>>> > Also the console.log shows that the inner log is never called. >>> >>>> > >>> >>>> > Cheers, >>> >>>> > Sebastian >>> >>>> > >>> >>>> > Am Dienstag, 23. Oktober 2012 15:47:49 UTC+2 schrieb Pawel >>> Kozlowski: >>> >>>> >> >>> >>>> >> Sebastian, >>> >>>> >> >>> >>>> >> the module.service accepts a _constructor_ function, which means >>> that >>> >>>> >> - while registering a service - AngularJS will call new on your >>> >>>> >> anonymous function, that is: >>> >>>> >> >>> >>>> >> new function ($http, $route) { >>> >>>> >> var url = Routing.generate('details', {id: >>> >>>> >> $route.current.params.id}); >>> >>>> >> return $http.get(url); >>> >>>> >> } >>> >>>> >> >>> >>>> >> and what will get registered is a result of your call (a >>> promise, to >>> >>>> >> be precise). So you are right in saying that your function will >>> be >>> >>>> >> called once only (!). >>> >>>> >> >>> >>>> >> What you could do is to simply register your function using a >>> factory >>> >>>> >> method: >>> >>>> >> >>> >>>> >> module.factory('IndexCntlResolver', ['$http', '$route', >>> >>>> >> function($http, >>> >>>> >> $route){ >>> >>>> >> >>> >>>> >> return function () { >>> >>>> >> var url = Routing.generate('details', {id: >>> >>>> >> $route.current.params.id}); >>> >>>> >> return $http.get(url); >>> >>>> >> } >>> >>>> >> }]); >>> >>>> >> >>> >>>> >> The above should work (didn't test, please send a plunk if you >>> need >>> >>>> >> more help with code!) but I guess it is simply not worth it, if >>> you >>> >>>> >> are not reusing this function in other parts of your >>> application. >>> >>>> >> >>> >>>> >> If you don't care about the reuse simply create a private >>> function >>> >>>> >> inside your configure block. Once again, please send a plunk >>> (links >>> >>>> >> in >>> >>>> >> my signature) if you need help with this. >>> >>>> >> >>> >>>> >> Cheers, >>> >>>> >> Pawel >>> >>>> >> >>> >>>> >> >>> >>>> >> On Tue, Oct 23, 2012 at 3:36 PM, Sebastian Bauer >>> >>>> >> <[email protected]> wrote: >>> >>>> >> > To make my application structure clean I'm working with >>> seperate >>> >>>> >> > modules >>> >>>> >> > for >>> >>>> >> > my controllers. On top I have one master controller for >>> >>>> >> > $routeChange >>> >>>> >> > event >>> >>>> >> > listeners. The routes are defined in the app config. >>> >>>> >> > >>> >>>> >> > I have something like this: >>> >>>> >> > >>> >>>> >> > angular.module('controllers.main', []) >>> >>>> >> > angular.module('controllers.details', []) >>> >>>> >> > angular.module('controllers.index', []) >>> >>>> >> > angular.module('app', ['controllers.main', >>> 'controllers.index', >>> >>>> >> > 'controllers.details']) >>> >>>> >> > >>> >>>> >> > I've read it's better to define everything in modules. Also >>> >>>> >> > controllers. >>> >>>> >> > And >>> >>>> >> > not with functions in the global namespace. The module way >>> seems >>> >>>> >> > pretty >>> >>>> >> > clean. >>> >>>> >> > >>> >>>> >> > For my routing I need resolve functions for the controllers. I >>> do >>> >>>> >> > some >>> >>>> >> > Ajax >>> >>>> >> > Calls there. Doing this with a global function is no problem: >>> >>>> >> > >>> >>>> >> > $routeProvider.when('/', { >>> >>>> >> > templateUrl: 'index.html', >>> >>>> >> > controller: 'IndexCntl', >>> >>>> >> > resolve: {httpData: IndexCntlResolver} >>> >>>> >> > }); >>> >>>> >> > >>> >>>> >> > Is there a way to put the resolver into a service? Or another >>> way >>> >>>> >> > to >>> >>>> >> > bind it >>> >>>> >> > to my controller module? >>> >>>> >> > >>> >>>> >> > Already tried to create a service. No success. It only runs >>> once. >>> >>>> >> > >>> >>>> >> > module.service('IndexCntlResolver', ['$http', '$route', >>> >>>> >> > function ($http, $route) { >>> >>>> >> > var url = Routing.generate('details', {id: >>> >>>> >> > $route.current.params.id}); >>> >>>> >> > return $http.get(url); >>> >>>> >> > } >>> >>>> >> > ]); >>> >>>> >> > >>> >>>> >> > I know services are singletons. But wondering because services >>> are >>> >>>> >> > accepted >>> >>>> >> > for resolve. I hope it's only my service definition that's >>> wrong. >>> >>>> >> > >>> >>>> >> > -- >>> >>>> >> > You received this message because you are subscribed to the >>> Google >>> >>>> >> > Groups >>> >>>> >> > "AngularJS" group. >>> >>>> >> > To post to this group, send email to [email protected]. >>> >>>> >> > To unsubscribe from this group, send email to >>> >>>> >> > [email protected]. >>> >>>> >> > Visit this group at >>> http://groups.google.com/group/angular?hl=en. >>> >>>> >> > >>> >>>> >> > >>> >>>> >> >>> >>>> >> >>> >>>> >> >>> >>>> >> -- >>> >>>> >> Question? Send a fiddle >>> >>>> >> (http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk >>> >>>> >> (http://plnkr.co/) >>> >>>> >> Need help with jsFiddle? Check this: >>> >>>> >> >>> >>>> >> >>> >>>> >> >>> http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/ >>> >>>> >> >>> >>>> >> Looking for UI widget library for AngularJS? Here you go: >>> >>>> >> http://angular-ui.github.com/ >>> >>>> > >>> >>>> > -- >>> >>>> > You received this message because you are subscribed to the >>> Google >>> >>>> > Groups >>> >>>> > "AngularJS" group. >>> >>>> > To post to this group, send email to [email protected]. >>> >>>> > To unsubscribe from this group, send email to >>> >>>> > [email protected]. >>> >>>> > Visit this group at http://groups.google.com/group/angular?hl=en. >>> >>> >>>> > >>> >>>> > >>> >>>> >>> >>>> >>> >>>> >>> >>>> -- >>> >>>> Question? Send a fiddle >>> >>>> (http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk >>> >>>> (http://plnkr.co/) >>> >>>> Need help with jsFiddle? Check this: >>> >>>> >>> >>>> >>> http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/ >>> >>>> >>> >>>> Looking for UI widget library for AngularJS? Here you go: >>> >>>> http://angular-ui.github.com/ >>> >>> >>> >>> -- >>> >>> You received this message because you are subscribed to the Google >>> Groups >>> >>> "AngularJS" group. >>> >>> To post to this group, send email to [email protected]. >>> >>> To unsubscribe from this group, send email to >>> >>> [email protected]. >>> >>> Visit this group at http://groups.google.com/group/angular?hl=en. >>> >>> >>> >>> >>> >> >>> >> >>> > -- >>> > You received this message because you are subscribed to the Google >>> Groups >>> > "AngularJS" group. >>> > To post to this group, send email to [email protected]. >>> > To unsubscribe from this group, send email to >>> > [email protected]. >>> > Visit this group at http://groups.google.com/group/angular?hl=en. >>> > >>> > >>> >>> >>> >>> -- >>> Question? Send a fiddle >>> (http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk >>> (http://plnkr.co/) >>> Need help with jsFiddle? Check this: >>> >>> http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/ >>> >>> Looking for UI widget library for AngularJS? Here you go: >>> http://angular-ui.github.com/ >>> >> -- 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.
