-
Notifications
You must be signed in to change notification settings - Fork 25.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(router): binding from router-outlet to child component #4452
Comments
👍 |
1 similar comment
👍 |
Perfect! As a current workaround, @cburgdorf and I introduced and are using "state" components. Those state components are just simply wrapper components that we navigate to and have the actual component that needs data through a binding as view child. |
👍 |
2 similar comments
👍 |
👍 |
I don't mean to ruin the party but since there may be several routes and therefore several completely different components behind That is why we recently introduced the concept of state components for a demo app. This is what @PascalPrecht was referring to. The reason we did that is so that we can build components that are kept free from routing concerns (e.g. |
👍 |
I totally need this feature or any idea how the point below could be implemented. Here is a plunk - http://plnkr.co/edit/ZCjWxIz1RSQtIqPfXps8?p=preview. The point is how |
@vsavkin could you look into this and pair with Brian on the best way to move forward? |
@vsavkin @yjbanov @IgorMinar and I just chatted about how to implement this. It'll take some changes to how @yjbanov – can you please explain the approach? |
It would work currently because the Is this a bug? |
Goal
Problem
One way we could deal with this problem is have parent publish the data via DI for child to inject instead of data binding. There are several problems with this:
Proposal
Example: @Component({ selector: 'foo-cmp' })
@View({
template: `<router-outlet [route-will-load]="passParams"></router-outlet>`,
directives: ROUTER_DIRECTIVES
})
@RouteConfig([
{ path: '/', component: ChildCmp }
])
class ParentCmp {
constructor(params: RouteParams) {
this.parentParams = params;
}
passParams(child: ChildCmp): void {
child.parentParams = this.parentParams;
}
} The implementation of NOTE: an alternative to a callback is to use an |
I think this API is a bit off as it stands. It's using a property-binding (not an event binding) to set up a function that gets called at a specific time, which is weird. Also, the param to Exploring a different API: @Component({ selector: 'foo-cmp' })
@View({
template: `<router-outlet></router-outlet>`,
directives: ROUTER_DIRECTIVES
})
@RouteConfig([
{ path: '/', component: ChildCmp }
])
class ParentCmp {
constructor(params: RouteParams) {
this.parentParams = params;
}
// Naming notwithstanding, just the concept.
@ChildRouteInit(ChildCmp, 'optional-specific-outlet-identifier')
passParams(child: ChildCmp): void {
child.parentParams = this.parentParams;
}
// Very similar to this, which we can do today.
@HostListener('click')
handleClick() { /***/ }
} Obviously it would also need a non-decorator syntax as well. |
cc @yjbanov @vsavkin @IgorMinar for thoughts on a) whether |
+1 @jelbourn idea |
👍 for something along these lines to pass data between routed components. |
Couple Ideas:
@Component({
inputs: ['state']
})
class MyPage {
state: { params: any, resolves: any}
}
@Component({ selector: 'child' })
class ChildCmp {
@Input() name;
}
@Component({ selector: 'foo' })
class FooCmp {
@Input() bar;
}
@Component({
selector: 'parent',
template: `
<router-outlet>
<child name="$params.name"></child>
<foo id="$params.id"></foo>
</router-outlet>`
})
@RouteConfig([
{ path: '/child/:name', component: ChildCmp },
{ path: '/foo/:id', component: FooCmp }
])
class ParentCmp {} |
The route outlet should obviously not be used to propagate bindings to instantiated components. What about the @RouteConfig and by specifying a |
or something like this
|
I just came across this issue as I was looking for a way to pass data to a routed child component. The given workaround above doesn't seem to work in dev mode as I get an exception when changing the child's property in I'm not sure if I've done anything wrong, but if not this issue is pretty important and should be addressed soon. It seems this was also discussed in #6094. I would also like to propose another syntax: @RouteConfig([
{ path: '/child/:name', component: ChildCmp, bindings: {'[childProp]': 'parentProp'} },
])
class ParentCmp {
parentProp: string = 'Hello World';
} This would allow to bind any input the child component might have just like you would if you would use the component right in the template. |
And what is solution for it? How can i get access navbar in childs from |
@zoechi I'd hate to hijack the thread but is there any similar approach for angular 1.5 where we can't create service instances for a specific component tree? |
@michal-filip Sorry, no knowledge about 1.5 |
I tried implementing your solution, but I don't think it's feasible. The |
The old new router has been deprecated, so it shouldn't be a issue anymore. You can't query dynamic loaded routes anymore since beta.16
That If you need to pass data to your routed componentes you must use services. But again, this old new router has been deprecated, stay tuned for the new new router. |
@ericmartinezr Thanks for the response. It has cleared up some things, but made others more confusing.
|
The "router formerly known as new" is no more. It is deprecated. It is an ex-router. It has ceased to be. (the name switch-a-roo is happening right now) The new "alt" router which will henceforth just likely be called "router" is similar in some ways, different in others. Until it's done and available to use it's hard to say what it will be exactly but it looks like there will still be router-outlets and the need to pass things via services. |
@maximedupre answering your questions
|
What about using the child component that was received in the |
@maximedupre to be honest this issue shouldn't be used a conversation forum, if you have any doubts about how to use the router you could go to gitter's chatroom and ask over there (https://gitter.im/angular/angular). This issue is closed and it will be buried with time. |
Need something like |
@sharpmachine use a shared service |
@zoechi How? If I'm not using a selector for a component because I want to use router-outlet, then how can I use a service to shared data? |
I don't see how using a selector is related. See https://angular.io/docs/ts/latest/cookbook/component-communication.html. If the component is added to a router-outlet and has a constructor parameter then DI looks up the parent components for a provider. If it finds one it requests its instance and passes it to the constructor. |
@zoechi I'm lost I'm doing that and it's not working for me. Parent:
Child:
This doesn't work. I can't get subject in the SubjectDetailComponent. I could try to call the same service method ( |
don't add This is not the right place for such discussions. Please post further questions at StackOverflow, Gitter, Google groups,... |
@zoechi I will do a stackoverflow but if it's not possible to do this via router-outlet, then it's an issue that needs to be discussed here. |
I'm facing the exact same issue. I've a resolver which resolves the first parameter info related and in the second resolver the param is not present. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
For example:
RouterOutlet
should be able to pass theprop-passed-to-child
binding toChildCmp
.The text was updated successfully, but these errors were encountered: