Suppose we have a couple of components that each of them should have different layout/ui (templates) conditioned with display dimensions.
My solution is to create such a component:
import {Component} from '@angular/core';
import {BreakpointObserver, Breakpoints} from '@angular/cdk/layout';
@Component({
selector: 'ui-switcher',
template: `
<ng-content *ngIf="isSmall" select="mobile"></ng-content>
<ng-content *ngIf="!isSmall" select="web"></ng-content>
`
})
export class UiSwitcherComponent {
public isSmall: boolean;
constructor(breakpointObserver: BreakpointObserver) {
breakpointObserver.observe([Breakpoints.Small, Breakpoints.XSmall]).subscribe(result => {
this.isSmall = result.matches;
});
}
}
and use it in this way:
<ui-switcher>
<web>
<!-- a ui suited for large screens -->
</web>
<mobile>
<!-- a very different ui suited for small mobile screen displays-->
</mobile>
</ui-switcher>
This solution may have some pitfalls. For example we can't use same templaterefs in <mobile>
and <web>
sections. (above we used #searchInput
and #searchInput2
).
What is the best practices for such situations?