javascript AngularJS 真的是 MVC 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13247738/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:10:46  来源:igfitidea点击:

Is AngularJS really MVC?

javascriptmodel-view-controllerangularjs

提问by Paul Lysak

I'm looking through examples on the AngularJS home page— specifically “Add Some Control”. I don't quite understand how it maps to MVC pattern.

我正在浏览AngularJS 主页示例——特别是“添加一些控件”。我不太明白它如何映射到 MVC 模式。

It's more or less clear that the template (index.html) can be thought of as the view, objects constructed by TodoCtrl from todo.js as the controller, but where is a model? Attributes like ng-modelmap to some internal pieces of the framework and don't directly expose an object which we could call a model.

模板(index.html)可以被认为是视图,由TodoCtrl从todo.js构造的对象作为控制器或多或少清楚,但模型在哪里?像ng-model映射到框架的一些内部部分这样的属性不直接暴露我们可以称为模型的对象。

Is it correct to call AngularJS an MVC framework?

将 AngularJS 称为 MVC 框架是否正确?

采纳答案by Jani Hartikainen

The attributes map into the scope variable as defined in the controller. For example, if you have some JS objects which represent your business logic, you can put those in the scope variable, and then the attributes can be mapped into the model itself instead of some arbitrary values in the scope variable.

属性映射到控制器中定义的范围变量。例如,如果您有一些 JS 对象代表您的业务逻辑,您可以将它们放在范围变量中,然后属性可以映射到模型本身而不是范围变量中的一些任意值。

回答by Reza

The core idea behind MVC is that you have clear separation in your code between managing its data (model), the application logic (controller), and presenting the data to the user (view). The view gets data from the model to display to the user. When a user interacts with the application by clicking or typing, the controller responds by changing data in the model. Finally, the model notifies the view that a change has occurred so that it can update what it displays. In Angular applications, the viewis the Document Object Model (DOM), the controllersare JavaScript classes, and the modeldata is stored in object properties.

MVC 背后的核心思想是在代码中明确分离管理数据(模型)、应用程序逻辑(控制器)和向用户呈现数据(视图)。视图从模型中获取数据以显示给用户。当用户通过单击或键入与应用程序交互时,控制器通过更改模型中的数据来响应。最后,模型通知视图发生了变化,以便它可以更新显示的内容。 在 Angular 应用程序中视图是文档对象模型(DOM),控制器是 JavaScript 类,模型数据存储在对象属性中。

回答by user1628524

I havent read up on Angular. However, keep in mind that "true" MVC means that the Model contains business Logic, the Controller takes care of communicating user input to the Model, and the View gets its own data from the Model. Many so called MVC frameworks are actually not implementing MVC the right way: They are overloading the Controller With to much responsibility, as in making the Controller responsible also to update the View. That is not what the Controller was intended to do, and in such cases you get what is called a "fat Controller". There is a misconseption among many webdevelopers that the View is simply a "template". This is both true and false; It is a template but it get its own data directly from the Model - not via the Controller.

我还没有读过 Angular。但是,请记住,“真正的”MVC 意味着模型包含业务逻辑,控制器负责将用户输入传送到模型,而视图从模型中获取自己的数据。许多所谓的 MVC 框架实际上并没有以正确的方式实现 MVC:它们使控制器重载了很多责任,就像使控制器也负责更新视图一样。这不是控制器的意图,在这种情况下,您会得到所谓的“胖控制器”。许多 web 开发人员存在一种误解,认为 View 只是一个“模板”。这是对的,也是错的;它是一个模板,但它直接从模型中获取自己的数据——而不是通过控制器。

But as I started out saying: I havent yet read up on Angular, but you might have so yourself and you should be able to find out if Angular is truly a MVC framework by looking at what the different Components in the Framework does.

但正如我开始所说的那样:我还没有读过 Angular,但你自己可能已经读过,你应该能够通过查看框架中不同组件的作用来确定 Angular 是否真的是一个 MVC 框架。

回答by user3738936

If anything that can store data in a object is considered a model than every front end framework (view) could be considered to have a model. They can all store data in objects and present them in the view. I think calling angular a mvc is really a marketing ploy, trying to make angular seem like it is more than it is.

如果任何可以在对象中存储数据的东西都被视为模型,那么每个前端框架(视图)都可以被视为具有模型。它们都可以将数据存储在对象中并在视图中呈现它们。我认为将 angular 称为 mvc 确实是一种营销策略,试图让 angular 看起来不仅仅是它。

See: https://www.pluralsight.com/blog/software-development/tutorial-angularjs-mvc-implementation

请参阅:https: //www.pluralsight.com/blog/software-development/tutorial-angularjs-mvc-implementation

In the controller, is where they give their example of angular's as a model. If it can't assist in the storage of persistent data it isn't much of a model.

在控制器中,他们给出了角度作为模型的示例。如果它不能帮助存储持久数据,那么它就不是一个模型。