为什么将Angular称为MV *框架 [英] Why is Angular called MV* framework

查看:100
本文介绍了为什么将Angular称为MV *框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我到目前为止所读的内容(此 answer 准确),它更倾向于MVVM模式.将来自服务的数据视为模型,将角度控制器视为VM,将包含角度绑定的HTML视为视图.

From what I've read until now (this answer precisely), it tends more towards MVVM pattern. Considering the data from services as Model, Angular controllers as VM and the HTML containing angular bindings as the View.

我们可以说MVC是服务器端的模式,而MVVM是客户端的模式吗?

Can we say that MVC is a pattern for server side and MVVM for client side?

有人可以举例说明(在Angular中)我们如何 实现MVC和MVVM.

Can someone explain with example (in context of Angular) how we can implement MVC and MVVM.

我已经阅读了使用var vm = this的概念;在角度控制器中.但这是否意味着仅对控制器使用别名即可将Angular应用程序从MVC转换为MVVM.

I've read about the concept of using var vm = this; in Angular controllers. But does it mean by just using an alias for our controller we convert our Angular app from MVC to MVVM.

推荐答案

首先,我认为,最好对 MVC MVVM 有所了解.

First I think, It is better to give some idea about MVC and MVVM.

不只是在更多的理论背景下进行描述.我宁愿用一个简单的例子来解释.让我们来买一个比萨饼.

More than describing on much more theoretical context. I'd rather explain with a simple example. Lets take buying a pizza.

MVC-当您致电披萨中心并送货时,情况类似.

  • 您呼叫呼叫中心负责人(控制器)并下达订单(输入).
  • 然后,呼叫中心负责人(控制器)安排一些厨师(模型)来制作披萨,然后安排递送人(查看)来递送披萨.
  • 然后,送货员(视图)从Cooks(模型)那里获取披萨,将其包装好(输出),然后送货给您.
  • You call a Call Center Guy(Controller) and make an order(input).
  • Then the Call Center Guy(Controller) arranges Some Cooks(Model) to make the pizza, a Delivery Guy(View) to deliver the pizza.
  • Then Delivery Guy(View) gets the pizza from Cooks(Model), wrap it nicely(output) and deliver to you.

MVVM-更像是,当您去商店并将订单下达给服务员时,会发生什么情况.

  • 您将订单(输入)下达到服务员(查看).
  • 您向咖啡厅经理订购的服务员(视图)(视图模型).
  • Cafe Manager(视图模型)安排一些厨师(模型)来制作披萨.
  • Cooks(模型)已准备就绪,并将其传递给Cafe Manager(视图模型).
  • 然后,Cafe Manager(视图模型)将其放入一个漂亮的盘子中,添加叉子/刀子,调料器等.(演示文稿).
  • 服务员(视图)跟踪咖啡馆管理器(视图模型).准备就绪后,Waiter(View)将交付给您.
  • You place your order(input) to the Waiter(View).
  • The Waiter(View) place you order to the Cafe Manager(View Model).
  • The Cafe Manager(View Model) arranges some Cooks(Model) to make the pizza.
  • Cooks(Model) makes it ready and pass it to the Cafe Manager(View Model).
  • Then the Cafe Manager(View Model) puts it into a nice plate, adds forks/knives, sauce dispenser etc. (Presentation).
  • Waiter(View) keeps track of Cafe Manager(View Model). Once it's ready, then the Waiter(View) delivers to you.

回到您的问题时,

我们可以说MVC是服务器端的模式,而MVVM是客户端的模式 一边?

Can we say that MVC is a pattern for server side and MVVM for client side?

我可以说的是通常是. (可能会有一些极端情况).希望您可以使用我的上述解释来更好地解决问题.

What I can say is Generally Yes. (might have some corner cases). I hope, you can use my above explanation to better deal in depth of your problem.

此外,由于您是指AngularJS,因此在体系结构中,它与 MVVM 非常接近(我这样说是因为它更像是没有答案).尽管我们在AngularJS中有 Controllers ,但实际上它们确实可以完成 View-Models 的工作.

Addition to that since you are referring about AngularJS, in architecture, it is much close to MVVM (I am telling like that because it is more like there's no answer). Though we have Controllers in AngularJS, actually they exactly do the work of View-Models.

----------使用AngularJS特定示例进行更新----------

由于我想将我们的范围保留在Angular体系结构内.我在举一个一般的例子.

Since I would like to remain our scope inside Angular architecture. I am taking a general example.

  • 您具有要使用AngularJS实现的组件的HTML模板. [查看]
  • 该HTML模板绑定到一个Controller,在其中,您可能会有类似this.controllerAs = vm的内容.实际上,该术语vm指的是视图模型. [视图模型]
  • You have your HTML template for the component you are going to implement with AngularJS. [View]
  • That HTML template is bound to a Controller, where inside that you would probably have something like this.controllerAs = vm. Actually this term vm refers to View-Model. [View-Model]

理想情况下,在此控制器内,我们不应该实施业务专用逻辑.如果希望它们包含在客户端中,则应具有单独的工厂,服务(自定义)等.您可以做的是,可以将那些(工厂,服务)包括在控制器内,并调用其所需的功能/方法来执行所需的操作.否则,您可以考虑在服务器端使用业务逻辑,并使用内置服务(例如:$ http)来调用它们.

Ideally inside this controller, we should not implement business specific logic. If you want them to be included in the client side, you should have separate Factories, Services (custom) etc. to do that. What you can do is, you can include those (Factories, Services) inside the controller and call their required functions/methods to perform the operation required. Otherwise you can consider having your business logic in the server side and using inbuilt services(Ex: $http) to call them.

因此,在控制器内部,我们仅具有实现所需视图逻辑(显示要求)的实现.

So Inside the controller we only have the implementations bound to view logic (display requirements) we want.

  • 因此,正如我在第二点提到的那样,您可以使用[自定义工厂,服务]或[REST服务集+ $ http],其中包含您的业务逻辑. [型号]

所以在交流流程中真正发生的是

So inside the communication flow what really happens is,

  • 客户端(最终用户/另一个组件)最好使用一些输入来调用/启动HTML( View ).
  • 然后,控制器( View-Model )获取输入并知道所需的任务是什么.
  • 然后,控制器在其(模型)中调用工厂,服务等,以准备绑定到给定输入的所需的特定于业务的输出.
  • 然后模型处理输入并将所需的输出提供给控制器.
  • 然后控制器对显示进行一些调整.
  • 然后将显示HTML.
  • A client(end user/another component) calls/initiates the HTML(View) preferably with some input.
  • Then the controller(View-Model) gets the inputs and knows what the required task is.
  • Then the controller calls the Factories, Services etc. inside the it(Model) to prepare the desired business specific output bound to the given input.
  • The Model then processes the input and gives the desired output to the controller.
  • Then the controller makes some display specific adjustments.
  • Then the HTML will display.

这篇关于为什么将Angular称为MV *框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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