templateUrl vs template in AngularJS

眉间皱痕 提交于 2019-12-12 16:44:29

问题


I think for routing, templateUrl should always be used over template. But for a directive, when should templateUrl be used instead of template? I see some people use chunks of HTML in string format as template, whereas others link all their directives to a templateUrl, even if the template is really simple and small.

So, what is the best practice for deciding which one to use? I am personally more in favour of using templateUrl regardless of the template's complexity, just because I hate putting HTML in strings, but I would love to learn about reasons beyond just subjective emotions.


回答1:


It cones down to preference, really. In general I agree with you, and more complex markup always makes more sense in a separate template due to ease of editing.




回答2:


I wrote a blog post about this: http://www.smarpshare.com/angular-template-vs-templateurl-the-move-toward-true-modular-development/ . In a nutshell, I find it is better to use dependency management system and template to organize your Angular project; it will help you to migrate to Angular 2 too!




回答3:


If you using lots of template in your project like SPA application with huge html rendered going on inside each template and of-course if you want to manage your application well for future then keep it separate.



来源:https://stackoverflow.com/questions/22117541/templateurl-vs-template-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!