javascript AngularJS 客户端数据绑定和服务器端模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13103050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS client side data binding and server side templating
提问by PrimosK
AngularJS uses two-way client side data binding (from AngularJS Developers guide):
AngularJS 使用双向客户端数据绑定(来自 AngularJS 开发人员指南):
Has anyone consider using mix of server side templating enginewith AngularJS two-way client side data binding. Something like this:
有没有人考虑将服务器端模板引擎与AngularJS 双向客户端数据绑定混合使用。像这样的东西:
I am thinking about using AngularJS just for parts(components) of the page? Would it be good idea?
我正在考虑将 AngularJS 仅用于页面的部分(组件)?这会是个好主意吗?
I would like to hear if you already had experiences with similar approach and what were drawbacks and advantages...
我想听听您是否已经有类似方法的经验以及有哪些缺点和优点......
采纳答案by Ben Lesh
Angular is a complete UI rendering client framework. You can feed data into it, and it will render the proper html. On it's own Angular is a templating solution completely de-coupled from any server.
Angular 是一个完整的 UI 渲染客户端框架。您可以将数据输入其中,它会呈现正确的 html。Angular 本身就是一个模板解决方案,与任何服务器完全分离。
What you're attempting to do, is re-couple your Angular application to your server.It will be more work, there will be very few benefits, and you'll lose your ability to switch server technologies but keep your angular application untouched.
您尝试做的是将您的 Angular 应用程序重新耦合到您的服务器。这将是更多的工作,将带来的好处很少,并且您将失去切换服务器技术的能力,但不会影响您的 Angular 应用程序。
In essence, you don't want to do this for the same reason you wouldn't want to have your server rendering JavaScript for you... it's just less obvious because of all of the years we've been rendering all of our HTML at the server.
从本质上讲,您不想这样做的原因与您不想让服务器为您呈现 JavaScript 的原因相同……这不太明显,因为多年来我们一直在呈现我们所有的 HTML在服务器上。
The end result is what's important. That the UI works properly and the application is easy to maintain. If you find your solution to be easier to maintain, then you should do it. I have a hard time imagining much being easier to maintain than straight HTML and JavaScript with a server that does nothing but host JSON.
最终的结果才是最重要的。UI 工作正常,应用程序易于维护。如果您发现您的解决方案更易于维护,那么您应该这样做。我很难想象比直接使用 HTML 和 JavaScript 的服务器更容易维护,该服务器除了托管 JSON 之外什么都不做。
回答by Mohamed Adel El-Badry
I had the same dilemma as you have, I come from a background of using SMARTY as a server side template engine and we recently started using AngularJS.
我和你有同样的困境,我来自使用 SMARTY 作为服务器端模板引擎的背景,我们最近开始使用 AngularJS。
I think mixing both solution would be appropriate in the the essence of total separation. i.e don't mix part of your page with both technologies but use both technologies separately.
我认为在完全分离的本质上混合两种解决方案是合适的。即不要将您的页面的一部分与这两种技术混合使用,而是单独使用这两种技术。
For instance if you have a listing page where users will not interact with it at all, you may use server side template perfectly.
例如,如果您有一个用户根本不会与之交互的列表页面,则可以完美地使用服务器端模板。
But if a page involve lots of manipulation and user side interaction in this case Angular is what you should use.
但是,如果页面在这种情况下涉及大量操作和用户端交互,那么您应该使用 Angular。
If you are using SPA (single page application) don't use server side template at all.
如果您使用的是 SPA(单页应用程序),则根本不要使用服务器端模板。
回答by fenta
In a cordova app we recently made, we used a kind of a mix. Fix content like forms, lists, headers and menues we did in the angular way, some parts like content from an RTE or often changing templates we added with the directive ngBindHtml. The idea behind is to be flexible in the content pages of the app if the client want to change layout or want to put currently unknown content to it. But i would suggest: if you can easy maintain the app (no long deployment/approval process) you should do it the angular way (but with no SEO in mind;).
在我们最近制作的cordova 应用程序中,我们使用了一种混合。修复我们以 angular 方式所做的表单、列表、标题和菜单等内容,某些部分如来自 RTE 的内容或我们使用指令ngBindHtml添加的经常更改的模板。如果客户端想要更改布局或想要将当前未知的内容放入其中,则背后的想法是在应用程序的内容页面中保持灵活。但我建议:如果您可以轻松维护应用程序(没有冗长的部署/批准过程),您应该以有角度的方式进行(但不要考虑 SEO;)。