简体   繁体   English

如何在Angular 2,Ionic 2的组件中动态更新模板

[英]How to dynamically update template in component of Angular 2, Ionic 2

I have a component that gets its template from a remote url. 我有一个从远程URL获取其模板的组件。 I want to have a function on event which does something to request the component's template again and change the already rendered template. 我想在事件上添加一个函数,该函数可以执行一些操作以再次请求组件的模板并更改已渲染的模板。

 @Component({ selector: 'cartpage-blocks-cartpage', templateUrl: '/url/to/romote/server/template' }) export class CartPageBlocksCartPage{ /** * Constructor of class * * @param events - events controller */ constructor( public events: Events ){ this.events.subscribe('reload-component-template', () => { /* Do something to reload template */ }); } } 

How to request component template again and recompile the template to a newer updated one? 如何再次请求组件模板并将模板重新编译为更新的模板? I want it because, after my user is authorised, my template automatically changes and I want to show changes dynamically by calling an event. 我想要它是因为在授权用户之后,我的模板会自动更改,并且我想通过调用事件来动态显示更改。

I think they have never intend to add dynamic swapping for templates, instead their recommended way is to have variables to swap the different views. 我认为他们从来没有打算为模板添加动态交换,而是建议的方法是让变量交换不同的视图。

A better way is to work with your routes and creating specific component templates with conditional statements to show/hide different features. 更好的方法是使用您的路线,并使用条件语句创建特定的组件模板以显示/隐藏不同的功能。 Make sure you set up AuthGuards to make sure that your app only allows authorised users. 确保设置AuthGuard,以确保您的应用仅允许授权用户使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular 2组件中动态加载外部模板? - How to dynamically load external template in Angular 2 Component? 如何在 angular html 模板中动态更改离子元素的属性? - How to dynamically change property of an Ionic element in angular html template? Angular 4动态更新组件视图? - Angular 4 Dynamically Update Component View? 如何在Angular2中的模板html上动态创建数组组件? - How to rendered dynamically created array component on template html in Angular2? 如何在其他组件中动态插入template-ref角度5 - How to insert a template-ref in other component dynamically angular 5 如何更新 Angular 中动态添加的子组件中的值 9 - How to update a value in the dynamically added child component in Angular 9 如何使用 MatDialogRef 重新加载/更新 Angular 中组件的模板 - How to reload/update a template of a component in Angular using MatDialogRef Angular 4/5可观察-如何根据可观察属性更新组件模板? - Angular 4/5 Observable - How to update component template depending on observable property? 在组件中动态加载角度模板字符串 - Dynamically load angular template string in component 在html模板angular 2中动态设置组件的属性 - Dynamically set attribute on component in html template angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM