[英]How to refer 2 template html file (mobile and desktop) dynamically in single component in angular 6?
My approach is as below. 我的方法如下。
Folder structure 资料夹结构
mobile-view.component.html mobile-view.component.html
<p> this is mobile view</p>
desktop-view.component.html desktop-view.component.html
<p> this is desktop view</p>
mobile.component.ts mobile.component.ts
import { BaseComponent } from './base.component';
@Component({
selector: 'app-mobile-view',
templateUrl: './mobile-view.component.html'
})
export class MobileComponent extends BaseComponent { }
desktop.component.ts desktop.component.ts
import { BaseComponent } from './base.component';
@Component({
selector: 'app-desktop-view',
templateUrl: './desktop-view.component.html'
})
export class DesktopComponent extends BaseComponent { }
base.component.ts base.component.ts
@Component({
selector: 'app-root',
template: `<app-mobile-view *ngIf="isMobileView"></app-mobile-view>
<app-desktop-view *ngIf="!isMobileView"></app-desktop-view>`
})
export class BaseComponent implements {
isMobileView: boolean;
constructor(){
if (navigator.userAgent &&
(navigator.userAgent.match(/Android/i) ||
navigator.userAgent.match(/webOS/i) ||
navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/BlackBerry/i) ||
navigator.userAgent.match(/Windows Phone/i))) {
this.isMobileView = true;
} else {
this.isMobileView = false;
}
}
ngOnInit() {
// code
}
// various methods
}
In this approach my main logic and all binding variables reside in base.component.ts 在这种方法中,我的主要逻辑和所有绑定变量都位于base.component.ts中
Mobile component and Desktop component extends Base component to access all methods and variables 移动组件和桌面组件扩展了基本组件以访问所有方法和变量
Note:- This is demo code just for understanding what i have tried. 注意:-这是演示代码,仅用于了解我的尝试。
Requirement : 要求:
Need any better approach or standard practice for achieving this 需要任何更好的方法或标准实践来实现这一目标
Need a way to set template dynamically based on device from which it is request. 需要一种根据请求设备动态设置模板的方法。 Refer base.component.html constructor for code of getting userAgent(provide info of device from which request is comming) which i am using currently.
请参阅base.component.html构造函数以获取获取我当前正在使用的userAgent(请求来自的设备的提供信息)的代码。
Previous method used:- 以前使用的方法:
main.component.html main.component.html
@Component({
selector: 'app-root',
template: function () {
if (isMobileUser()) {
return require('./mobile-view.component.html');
} else {
return require('./desktop-view.component.html');
}
}(),
styleUrls: ['./main.component.scss']
})
export class MainComponent {
}
You can follow this approach which is simpler when compared to above one. 与上面的方法相比,您可以采用这种方法更简单。 using ngContainer with ngTemplateoutlet, you can inject the template into the container based on condition.
结合使用ngContainer和ngTemplateoutlet,可以根据条件将模板注入到容器中。
@Component({
selector: 'app-mobiledesktop-view',
templateUrl: './mobiledesktop-view.component.html'
})
export class MobileDesktopComponent { }
**Template:**
<ng-template #mobileView>
This is mobile view
</ng-template>
<ng-template #desktopView>
This is desktop view
</ng-template>
<ng-container *ngTemplateOutlet="isDesktop ? desktopView : mobileView">
</ng-container>
Using Dynamic Component: 使用动态组件:
@Component({
selector: 'app-desktop-view',
template: 'This is desktop view'
})
export class AppDesktopComponent { }
@Component({
selector: 'app-mobile-view',
template: 'This is mobile view'
})
export class AppMobileComponent { }
@Component({
selector: 'app-container-view',
template: ' <ng-container #messagecontainer></ng-container>'
})
export class AppContainerComponent {
private componentRef;
@ViewChild('messagecontainer', { read: ViewContainerRef }) entry: ViewContainerRef;
constructor(private resolver: ComponentFactoryResolver) { }
ngAfterViewInit(){
const component = (isDesktop) ? AppDesktopComponent : AppMobileComponent;
const factory = this.resolver.resolveComponentFactory(component);
this.componentRef = this.entry.createComponent(factory);
//this.componentRef.instance.data = appData;
}
ngOnDestroy() {
this.componentRef.destroy();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.