简体   繁体   English

如何检查angular2中的templateUrl是否有效?

[英]How do check if the templateUrl is valid in angular2?

I build my views using the C# Helpers and angular2 consume this view by call a controller and receiving the html of view generated by C#. 我使用C#助手构建视图,angular2通过调用控制器并接收C#生成的HTML视图来使用此视图。 But my problem is when the user have no permissions to access the view and the controller returns 401 Unautorized, this produce a error when angular try to template load and the component stay broken until you clean cache and reload with valid authorization. 但是我的问题是,当用户没有访问该视图的权限并且控制器返回401 Unautorized时,这会在尝试对模板加载进行有角度的尝试并且组件保持损坏直到您清理缓存并使用有效授权重新加载时产生错误。

Any idea that the how can i validate templateUrl before component load ? 知道在组件加载之前如何验证templateUrl的想法吗?

I resolved my problem creating a function that is call on templateUrl and return the correct string url. 我解决了创建在templateUrl上调用并返回正确的字符串url的函数的问题。

 function hasTemplate(template: string) { var http = new XMLHttpRequest(); http.open('GET', template, false); http.send(); return (http.status === 200) ? template : '/view/home/error/'; } export const getViewUrl = <TModel>(modelo: { new (): TModel }, tipo: string) => { //Url to call web service and verify the response let url = `/view/${modelo.name.toLowerCase()}/${tipo}`; return hasTemplate(url); }; @Component({ selector: 'cidade-list', templateUrl: getViewUrl(Cidade, 'list'), styleUrls: [CssPath], providers: [ CidadeService, ] }) export class CidadeListComponent {} 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM