剃刀模板,观点和angular.js [英] Razor templates, views and angular.js

查看:127
本文介绍了剃刀模板,观点和angular.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用.NET剃刀意见和AngularJS时的最佳实践?

What are the best practices when using .NET Razor views and AngularJS?

我们正在开发使用mvc4剃刀一个公开网站(而不是Intranet应用程序),而我们不是很熟悉的客户端脚本,所以我们开始与我们知道:jQuery的。
但现在事情变得更加复杂,我们想切换到AngularJS。

We are developing a public website (not an intranet application) using mvc4 with razor, and we weren't very familiar with client script, so we started with what we knew: jQuery. But now things are getting more complicated and we'd like to switch to AngularJS.

在.NET的一部分,我们用剃刀模板和 UIHintAttribute (加上一些自定义的)来渲染HTML权的控制。我们还添加自定义HTML属性给予额外的信息,jQuery的部分(如标题的提示....)

On the .NET part, we use Razor templates and UIHintAttribute (plus some custom ones) to render the right html "control". We also add custom html attributes to give extra information to the jQuery part (like title for a tooltip....)

因此​​,我们已经使用设置用户界面行为的声明方式,这就是为什么AngularJS似乎是一个不错的选择。

So we already use a declarative way of setting the user interface behavior, that's why AngularJS seems a good option.

由于我们已经定义服务器端的机型,而且由于AngularJS也使用模式,是不是逼我们复制code?

Since we already have models defined server side, and since AngularJS also uses models, wouldn't it force us to duplicate code?

我们如何处理数据绑定功能,因为我们已经做了一些结合服务器端(在视图中)。我们应该做一个完全异步的应用,使得AJAX从AngularJS调用加载数据,或者我们可以混合两者兼而有之?

How do we deal with data binding feature, since we already do some binding server side (in the views). Should we make a completely asynchronous application, making AJAX calls from AngularJS to load data, or can we mix both?

还有什么我们应该意识到试图利用这两种技术的时候?

Anything else we should be aware of when trying to use both of these technologies?

我做了谷歌的一些研究,但我找不到混合剃刀意见和模板AngularJS ...详细的方法也许这只是不是一件好事呢?

I did some research on Google, but I can't find detailed ways of mixing Razor views and templates with AngularJS... Perhaps that's just not a good thing to do?

推荐答案

我们用MVC加上另外的JavaScript框架(淘汰赛)工作时处理这个问题好几个月了。最后,如​​果你要使用渲染用户界面客户端的MV *框架,你会发现,大部分开沟剃刀将是你最好的选择。

We dealt with this issue for months when working with MVC plus another JavaScript framework (Knockout). Ultimately, if you're going to be using a client-side MV* framework for rendering your user interface, you will find that mostly ditching Razor is going to be your best bet.

大多数主要MV * JavaScript框架,包括AngularJS的,假设你将维护用户界面状态和基于JavaScript的模型或视图模型渲染用户界面。尝试在服务器端渲染混合只是不会工作得很好。

Most of the major MV* JavaScript frameworks, including AngularJS, assume you will be maintaining UI state and rendering your user interface based on JavaScript models or view models. Trying to mix in server-side rendering is just not going to work very well.

这并不是说有没有使用MVC,当涉及到开发一个角度应用程序。你仍然可以采取一些强大的功能优势,如 ASP.NET捆绑和缩小 。有时它的作品真的很好,直接使用的Razor视图或嵌入JSON到页面的部分,而不是做一个额外的AJAX调用。

That's not to say there is no use for MVC when it comes to developing an Angular application. You can still take advantage of some great features like ASP.NET Bundling and Minification. And sometimes it works really well to embed JSON directly into the page using a Razor view or partial as opposed to making an additional AJAX call.

至于车型,你可能想看看 Breeze.js 。这是一个JavaScript库进行数据访问的服务器端与ASP.NET去很大的共享模型元数据。

As for models, you may want to take a look at Breeze.js. It's a JavaScript library for data access that goes great with ASP.NET on the server side to share model metadata.

这篇关于剃刀模板,观点和angular.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆