-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Route to component: Allow binding to "&" for routed components #3239
Comments
See also #3111 for binding "&" to functions returned by resolve |
I commented on #3111 I think the usage and API of resolve is becoming overloaded and clunky supported this way. |
Documented parent/child state bindings: https://ui-router.github.io/guide/ng1/route-to-component#routed-parentchild-component-communication |
How can do this on ng-metadata? |
Using ui-router RC1, does the child need to have a child state of the parent in order to get the foo-binding on the Is it possible with: or do we need to have: I have |
As requested in #2627 (comment) , #3099 , and elsewhere, it could be useful to bind to a routed component's "&" output bindings.
Current Behavior: Map resolves to inputs
Currently, routed components only receive input bindings from resolve data. We currently map resolve data to the following types of bindings:
When routing to this component, resolve data is mapped to the bindings. Route-to-component uses a
templateProvider
which generates a template such as:This binds resolve data (named
input1
,input2
, andinput3
to the component's input bindings.The problem with output bindings
Output bindings for components in angular 1 are declared using a
"&"
. This instructs angular to create a proxy function on the component controller. When an output event occurs, the function is invoked.When creating an instance of the component in a parent component, the child component's output is wired to some functionality on the parent by writing an expression (which usually invokes a handler function on the parent component controller).
In the parent component:
When the button on the child is clicked, it calls the proxy function with the parameters (which it evaluates in the child component scope). Then the proxy function evaluates the wiring expression (
$ctrl.handleEvent(foo, bar)
), interpolating using the variables from the child (foo
andbar
), and finally invokes the expression in the parent component's scope (sohandleEvent
is evaluated in the parent component's scope).It's common to write components using this pattern where the parent component wires up event handlers in the parent to events emitted from the child. The child informs the parent of events and allows the parent to handle them. I.e.,
<with-output on-event="$ctrl.handleEvent(foo, bar)"><with-output>
However, once you decide that the child component
withOutput
should be routed, you can no longer use the same pattern because UI-Router does not allow bindings to cross theui-view
boundary. It currently only allows input bindings to receive resolve data.Proposal
To make this component routing model more flexible, the proposed solution is to pass through bindings which the child component declares, and which the parent component has wired up through the ui-view.
Example 1
The states using these components:
For example, if the child component declares an input component:
and the parent component has wired up
some-input
on theui-view
:Then the template generated for
some-child
by route-to-component will be:These wired bindings on the ui-view will take precedence over the existing auto-wiring of resolve data to component inputs.
Example 2
This example shows how to wire up an event emitted from a routed child component to a parent component, through the
ui-view
:The states using these components:
and the parent component has wired up a handler, via the
ui-view
:When the button in
some-child
is clicked, thehandleSomeEvent
in thesome-parent
is invoked.The html in the DOM when
parent.child
is active would be:Note that the extra
on-some-event
wiring on theui-view
remains, but causes no bad behavior because theui-view
itself doesn't process it.Limitations
Not all attributes on the
ui-view
from the parent component will be passed through to the child component. Only attributes on theui-view
which match a child component'sbindings
will be passed through. This is so you can use attributes or directives specific to theui-view
.This will probably not work with any sort of isolate scope on the
ui-view
. For example, it probably does not work if theui-view
has anng-if
toggling it.The text was updated successfully, but these errors were encountered: