You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a tab is never opened, its contents should never be added to the DOM. This is how things worked with alpha 9 on Angular 2.0.1
What is the current behavior?
Even if a tab is never opened, its content is added to the DOM before being removed.
What are the steps to reproduce?
This plunk shows the old behavior , from alpha 9. If you run the application while looking at network requests for images, you will see that only the image from the first tab is downloaded. To download another image, you need to open its tab.
What is the use-case or motivation for changing an existing behavior?
In my use case, I have a menu on the first tab and a video on each of several other tabs. Since I upgraded to alpha 10, the browser fetches all the videos at once instead of waiting for the user to open a tab. This results in excessive bandwidth usage.
The same problem would occur for an app that has different image galleries organized by tab
Which versions of Angular, Material, OS, browsers are affected?
Angular 2.1.2, Material 2 alpha 10 on Firefox 49
Reacted by Emerson Dallagnol, Alex, ahmedshabib and Llorenç Pujol Ferriol
changed the title [-][tabs @alpha.10] Even never-opened tabs get fully loaded: wasteful[/-][+][tabs @alpha.10] All tabs are loaded eagerly[/+]on Nov 15, 2016
It shouldn't load the content until the user click the tab.
I think the best approach is to load the content when the click event is fired and the loaded content is not destroyed anymore if the user switch to another tab and switch back again
Now I understand. It sounds like a separate issue from all tabs loading eagerly. In your shoes I would open a new issue and link to a demo of the problem.
@jelbourn did you have any more thoughts on this? As far as I can tell this bug was introduced with the Tabs API simplification in PR #1645. I have my own version of the tabs component(s) and just applied this change myself to try to fix the issue described in #573. It must be something to do with unwrapping the tab content from the <template> tags.
Anyone knows when this will be fixed? In my case upgrading from alpha 9 is just impossible know. Every tab in my app contain a component that contains more nested components and it takes ages to render. Furthermore if I change tab every component in every tab is created and immediatelly destroyed. Components are using services to fetch data when initialized so for example: if I have 6 tabs and movefrom one to another I have 6 requests in network.
This is actually because of the way Angular does content project ( adheres to the web-components spec ). Content inside ng-content will always instantiate even if its not projected to the DOM. Material tabs uses content projection ( https://github.com/angular/material2/blob/master/src/lib/tabs/tab.html#L4 ) to create the content template of the tab and then projects it into the DOM when the tab is activated.
A temp workaround is to put the inner content in a div and do an *ngIf with an expression that is true when the selected tab is activated.
@amcdnl Funnily enough when I first filed an issue on this topic I referred to Material's Tabs implementation as a good reference for working around this issue. An older implementation of the Tabs did not have this issue, as mentioned above. It seems to have been introduced when the Tabs API was simplified in PR #1645 - reverting the underlying logic to what it was before this PR restores the lazy-loading behaviour.
Activity
jelbourn commentedon Nov 15, 2016
@andrewseguin I think this might be a bug in Angular based on the way we're doing tabs.
[-][tabs @alpha.10] Even never-opened tabs get fully loaded: wasteful[/-][+][tabs @alpha.10] All tabs are loaded eagerly[/+]RoxKilly commentedon Nov 15, 2016
A dirty workaround: I listen to
(selectChange)and I wrap the tab contents in an*ngIfthat adds to the DOM only if the tab is currently selected.See this plunk
elvirdolic commentedon Nov 15, 2016
It shouldn't load the content until the user click the tab.
I think the best approach is to load the content when the click event is fired and the loaded content is not destroyed anymore if the user switch to another tab and switch back again
[-][tabs @alpha.10] All tabs are loaded eagerly[/-][+][Tabs] [tabs @alpha.10] All tabs are loaded eagerly[/+]esbeto commentedon Nov 16, 2016
Might be related: after upgrading to alpha.10
Tabs that have an *ngIf are breaking for me:
RoxKilly commentedon Nov 17, 2016
@esbeto my last demo (see my most recent post above) uses a working
*ngIfwithin a tabesbeto commentedon Nov 17, 2016
@RoxKilly There's something going on with Angular 2.2.0 and material tabs, that you can't use
*ngIfin a tab, like so:RoxKilly commentedon Nov 17, 2016
@esbeto Oh I see. I thought you meant:
Now I understand. It sounds like a separate issue from all tabs loading eagerly. In your shoes I would open a new issue and link to a demo of the problem.
jeffreyhofstede commentedon Nov 18, 2016
The same error occurs when using *ngIf in a md-menu-item in combination with webpack.
EXCEPTION: Cannot read property 'appendChild' of nullbenelliott commentedon Jan 3, 2017
@jelbourn did you have any more thoughts on this? As far as I can tell this bug was introduced with the Tabs API simplification in PR #1645. I have my own version of the tabs component(s) and just applied this change myself to try to fix the issue described in #573. It must be something to do with unwrapping the tab content from the
<template>tags.benelliott commentedon Jan 4, 2017
I reintroduced my equivalent of the
md-tab-contentdirective to fix the issue in my own version.mateuszwitkowski commentedon Jan 18, 2017
Anyone knows when this will be fixed? In my case upgrading from alpha 9 is just impossible know. Every tab in my app contain a component that contains more nested components and it takes ages to render. Furthermore if I change tab every component in every tab is created and immediatelly destroyed. Components are using services to fetch data when initialized so for example: if I have 6 tabs and movefrom one to another I have 6 requests in network.
6 remaining items
amcdnl commentedon Aug 13, 2017
This is actually because of the way Angular does content project ( adheres to the web-components spec ). Content inside
ng-contentwill always instantiate even if its not projected to the DOM. Material tabs uses content projection ( https://github.com/angular/material2/blob/master/src/lib/tabs/tab.html#L4 ) to create the content template of the tab and then projects it into the DOM when the tab is activated.A temp workaround is to put the inner content in a div and do an
*ngIfwith an expression that is true when the selected tab is activated.Reference: angular/angular#13921 (comment)
benelliott commentedon Aug 14, 2017
@amcdnl Funnily enough when I first filed an issue on this topic I referred to Material's Tabs implementation as a good reference for working around this issue. An older implementation of the Tabs did not have this issue, as mentioned above. It seems to have been introduced when the Tabs API was simplified in PR #1645 - reverting the underlying logic to what it was before this PR restores the lazy-loading behaviour.
amcdnl commentedon Aug 15, 2017
@benelliott - We are working with the core team to fix this behavior. Stay tuned for updates :)
[-][Tabs] [tabs @alpha.10] All tabs are loaded eagerly[/-][+][Tabs] Tabs are loaded eagerly[/+]pantonis commentedon Nov 14, 2017
@andrewseguin What is the expected behavior of this when it is fixed?
Only active tab content is added to DOM? Or all tabs content are added?
josephperrott commentedon Mar 7, 2018
Closing as tabs are no longer eagerly loaded. They are loaded when active and removed from the DOM otherwised.
mohyeid commentedon Aug 8, 2018
@josephperrott
Not sure why this is closed, but the issue still present
https://stackblitz.com/edit/angular-6tqpal
UPDATE: figured out I have to use
<ng-template matTabContent>and update example.kingeight commentedon Nov 15, 2018
Use the event 'selectedIndexChange' on mat-tab-group and put your content in a div with ngIf,like this
https://stackblitz.com/edit/angular-tu7jsg
angular-automatic-lock-bot commentedon Sep 10, 2019
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.