angular-ui / ui-router

The de-facto solution to flexible routing with nested views in AngularJS
http://ui-router.github.io/
MIT License
13.54k stars 3k forks source link

API to access to mounting element for state (uiView element in html) #3573

Closed falsyvalues closed 6 years ago

falsyvalues commented 6 years ago

This is a:

My version of UI-Router is: 1.0.11 ng1

Feature Request

Would be nice to have an access to list of mounting elements (uiViews) corresponding to state, possibly through existing views() API.

Code below is an example what can be achieved right now but contains some downsides (but illustrate the idea behind this):

$transitionsProvider.onStart({
    from: 'a.*',
    to: 'a.*'
}, (transition) => {
    const fromState = transition.from();

    if (typeof fromState.template === 'string') {
        // Returns as an exmaple "my-directive" it will looks different with components case
        const elementName = getElementNameFromTemplate(fromState.template);
       // There is no way to find proper uiView element as a root
        const elementInsideSomeUiView = angular.element(document).find(elementName);
        // Do anything with element
    }
});

General Query

Described in Feature Request.

christopherthielen commented 6 years ago

Here's what I recommend:

Plunker example: http://plnkr.co/edit/vPSZL8X78v8IUh4acorv?p=preview

I've also added an onSync callback to the view plugin API. I will release the new onSync api in a future release of ui-router. Plugin APIs are lower level than the rest of ui-router APIs and is more likely to change in the future. The onSync API will informs listeners of each $view.sync() event and provides the list of all active ui-view and the matching view configs (from state views block). This API may never provide an actual element as part of the api because it does not always make sense in all frameworks and environments (such as server-side rendering)

falsyvalues commented 6 years ago

Hi @christopherthielen thanks for looking into it. I guess example could be extended with some injectable registry of ui-views (managed via directive) which could be useful with onSync :tada:. Looking forward for onSync release.