ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

How to use $viewContentLoaded to get notified if a specific view has finished loading?

Open ghost opened this issue 11 years ago • 23 comments

Hi all,

I can't get my head around the $viewContentLoaded event. I have to views at my root state... mainContent and lightbox and I need to execute some code everytime the lightbox view has been updated.

For now I have the following code in the run method of my route module:

$rootScope.$on('$viewContentLoaded', function(event) {
    console.log(event);
});

But since there is only the event available I have no clue how to know for sure which view has been loaded, because this view is populated by many different controllers.

Any hints?

Best regards tpinne

ghost avatar Feb 20 '15 13:02 ghost

This seems like a valid issue. There isn't really anything to differentiate multiple views being loaded.

eddiemonge avatar Feb 21 '15 00:02 eddiemonge

It would be great to have at least the name of the view. But it would be awesome to have the element the view is attached to too :)

ghost avatar Feb 21 '15 14:02 ghost

+1

I would love to get the view name on $viewContentLoaded and $viewContentLoading events...

sscovil avatar Jul 09 '15 18:07 sscovil

+1

according to the docs, we should have it in viewConfig.targetView ... but really doesn't seem like targetView ever gets set.

I agree that it would also be nice to get some info in the $viewContentLoaded about what view and content was loaded.

plong0 avatar Aug 20 '15 02:08 plong0

I had a quick look into the ui-router code, and was able to set the name of the targetView set into the $viewContentLoading viewConfig.targetView

I thought that might be sufficient, but a few things:

  1. $viewContentLoading only seems to get fired the first time content is loaded into the view.
  2. $viewContentLoaded gets fired every time, but doesn't have any info about the view.
  3. it would be nice to know when a view gets emptied out (ie. state is not implementing that view)

I have a workaround that uses the $stateChangeSuccess event and inspects the toState.views object. This has the limitation that it does not account for view content loaded by parent views. I have it in mind that I could use $state.get() to list all states, then loop on them using $state.includes(toState) checking if any parent state is setting view content. Seems way more complex than it should be just to know if a view has content though.

plong0 avatar Aug 20 '15 02:08 plong0

How's this? https://github.com/angular-ui/ui-router/commit/f9b43d66833f0e17de41fd8d1cc3b491e3ba4a0e

nateabele avatar Aug 26 '15 16:08 nateabele

Looks really promising for sure. I think it could be nice if we could also get some information about what is loaded into the view (for example, the template) and maybe even include the state that is loading the view?

My use case is that I would like to detect if a view has content or not so that I can set a class on the body element. ex.

$rootScope.bodyClasses = [];
$rootScope.$on('$viewContentLoaded', function(event, viewName, viewContent){
    var hasViewClass = 'has-view-'+viewName;
    var bodyClassIndex = $rootScope.bodyClasses.indexOf(hasViewClass);

    // check if the view has content and add/remove the bodyClass
    if(viewContent && bodyClassIndex == -1){
        $rootScope.bodyClasses.push(hasViewClass);
    }
    else if(!viewContent && bodyClassIndex != -1){
        $rootScope.bodyClasses.splice(bodyClassIndex, 1);
    }
});
<body ng-class="bodyClasses">
...

plong0 avatar Aug 26 '15 16:08 plong0

+1 on this bug. Just spent two hours trying to figure out if there was an easy to get the name of the view that was loaded.

This feature would be really handy.

rohitpagariya avatar Sep 04 '15 01:09 rohitpagariya

I believe this is implemented on master.

nateabele avatar Sep 04 '15 02:09 nateabele

I don't understand the use cases for listening to view content loaded. What do you accomplish with the event?

christopherthielen avatar Sep 05 '15 20:09 christopherthielen

OK, here's my use case, I've just come across this, and the version of ui-router I have (0.2.15) gets the event but no name, but when I look at the latest code here on github it looks like it's only going to be sending the view name from now on. (edit: I read that code wrong... being it's an 'emit' that means the event is always sent as the first argument, so it looks like it should do what we want going forward)

So, my case is this. I am implementing a system where whenever a view changes, I want to capture the current state of it and send it off to the server (for a form of full resumption of current session in case a user's browser dies)... so I have just then written code like:

 var viewContentLoaded = function (event,viewName){
     console.log("View Content Loaded ["+viewName+"] ",event);
     eavesDropper.saveState(event.targetScope);
 };

based on the API, except viewName is always undefined. OK, not so bad, I can probably work out things via the targetScope.

I use the targetScope as:

 if(viewScope.hasOwnProperty("getState")){
      return viewScope.getState();
}

So, basically, if the controller that's been loaded into a view has a getState function, run it to get a JSON object defining what we'd need to know to resume back to our current state, and we're all good.

It means I can just put getState methods on controllers I want this functionality for, but no other code is required, and it doesn't matter if I forget it, it just won't be called.

But now I see it'll just get the viewName (which is something)... I don't know how to get the scope of a view by it's ui-view name alone.

You asked for a use case, this is one I just came up with while I was looking for a method to implement this current state saving mechanism with the least amount of intrusive code having to be written all over the place.

spoco2 avatar Sep 10 '15 02:09 spoco2

I have another use case. I want to record page load times for different views of my SPA. I want to achieve this by adding a callback to the requestAnimationFrame from the viewContentLoaded event. Having the viewName as well as other metadata about the state/view will help me record PLTs without instrumenting every controller's scope as mentioned in the comment above.

rohitpagariya avatar Sep 18 '15 23:09 rohitpagariya

@nateabele It is implemented on master but the release version stripped the , name see: https://github.com/angular-ui/ui-router/search?utf8=%E2%9C%93&q=viewContentLoaded&type=Code & https://github.com/angular-ui/ui-router/issues/2329

shennyg avatar Jan 06 '16 19:01 shennyg

Need this too.

rainabba avatar Mar 08 '16 08:03 rainabba

@shennyg Yeah, I'm not seeing it...

nateabele avatar Mar 08 '16 17:03 nateabele

In the meantime, in the controller for each view I am calling $scope.viewName="viewFoobar" manually so later I can just check $scope.viewName

Inelegant, but it works.

rainabba avatar Mar 08 '16 19:03 rainabba

This feature is mandatory to measure performances between loading & loaded properly without having to write markers on each controller view.

I use performance API to enhance browser's perf tools rendering.

Would be great to have that.

MarcLoupias avatar Jul 07 '16 14:07 MarcLoupias

Has there been any progress on getting the name back in the code? I could really benefit from being able to see the view name in $viewContentLoaded.

ghost avatar Jul 15 '16 19:07 ghost

+1 this would be super useful especially so for nested views, which I'm futzing with presently.

Slater-Victoroff avatar Nov 05 '16 17:11 Slater-Victoroff

+1

pciavald avatar Apr 29 '17 14:04 pciavald