Thanks a lot, that's actually pretty helpful. I guess I was zoomed in too
far; I was thinking "A route === one tangible thing"... but kind of missed
the obvious part of you can have multiple components per route.
Thanks.
On Friday, August 12, 2016 at 1:33:57 AM UTC-7, Zlatko Đurić wrote:
>
> Not sure if I understood correctly: you're saying that you want the route
> data on a service? I don't think that's how it works, and frankly, I don't
> see how it can work that way. If you have a service, that is by definition
> *not* tied to a single route, why would you want hardcoded (from
> service's point of view) data for a single route?
>
> The thing is, the service will, in theory, be called by multiple
> components, each with their own state and route state. So if you want
> route-specific information *in your service*, then that info needs to
> come from the source - that is, from the component actually rendered on the
> route. If it's a service then it should not be concerned with view or with
> app states much, it should only say "hey, I'm here, if you want some info,
> ask it from me, and if you want details, please give me your *someKey*".
>
> So you could design the service to take this *as a parameter* to whatever
> calls you're making to it. Have route call the service with the
> route-specific data as arguments, and then your service can decide what to
> do about it.
>
> To copy-paste your samples to illustrate what I mean:
> const CommonRoutes:RouterConfig = <Route[]>[
> {
> path: '',
> component: Home,
> data: {
> someKey: true
> }
> }
> ];
>
>
> @Component({
> })
> export class HomeComponent {
>
> private myData: any;
> constructor(private myService: MyService ) {
> this._route.data.subscribe(data => {
> console.log(data);
> this.myService.getDataByKey(data.someKey)
> })
> }
> }
>
>
> @Injectable()
> export class MyService {
> private data: any = {
> data1: 'value',
> data2: 'value2'
> };
>
>
> getDataByKey(someKey: boolean) {
> return someKey ? this.data.data1 : this.data.data2;
> }
> }
>
>
> Does that make any sense? Does it help in your use case?
>
--
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 https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.