39

I have worked in Angular 1.It clearly had a controller which acted as a mediator between View and Model. I realized that in Angular 2 we don't have any controller as such.

So can we still call Angular 2 as an MVC framework?. I know MVC is a design pattern and you implement it in any language. But, with respect to Angular 1, I heard from many that it is an MVC framework, and most of the examples that I saw clearly said that Angular 1 is MVC and Controller separates Model from View. So, I was wondering, now that have Components in Angular2, can we still call it as MVC? Or as Components by themselves follow MVC paradigm, because I do see that in each Component we do separate View and Data and use binding, maybe we can still call it as an MVC.

Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
Ram V
  • 724
  • 1
  • 8
  • 18
  • Not that this is much help, but I think it's referred to as an MV* (or MVWhatever) framework, so you have some license in how to implement the last part. – lintmouse Mar 03 '16 at 03:36
  • 1
    The angular2 documentation has a great overview of the architecture. It can be found here: https://angular.io/docs/ts/latest/guide/architecture.html. Although Angular2 is component based you still have separate views, separate scopes for your views, and services that allow you to connect to your backend. – user2263572 Mar 03 '16 at 03:53
  • 1
    I would definitely consider Angular2 MVC. Services would be your model, Components are a mix of Controller and View. You put most of your function logic and "Controller" functions within your class, and attach your view to it via templating. – Morgan G Mar 03 '16 at 04:30
  • Follow the link for explanation: [how-mvc-pattern-can-be-explained-in-angular](http://stackoverflow.com/questions/38844771/how-mvc-pattern-can-be-explained-in-angular-2/38845197#38845197) – Vishal Singh Aug 09 '16 at 09:06
  • 1
    @Morgan G Services are definitely not the model. In all MVC related frameworks (including MVVM etc) you have 2 models. The 1st is the domain model. The second is the model that adapts the domain model to the view. In MVVM this is the ViewModel. Many people have also started to use the term ViewModel in straight MVC. – gusgorman Jun 21 '17 at 08:36

3 Answers3

35

Angular 2 is more of a component based architecture. You can assume everything as component, like directives, services and so on. While directives and services are actually for the support of base components, they are also defined in a similar fashion. A base component contains dependencies, view details and a class declaration which may be considered as controller. So, a well defined component contain one set of MVC architecture.

for example (angular 2 alpha version) :

import {Component, View, bootstrap, provide, NgClass} from 'angular2/angular2';

@Component({
  selector : "my-home"
})

@View({
   directives : [NgClass, EditSettingPanel],
   styles: ['.hidden{ display : none} .visible{ display : block}'],
   templateUrl : "views/blog-panel.html"
})
export class home {
}

}

In the above example you can see that class "home" may be assumed as controller, View is written with @View decorator. The component customization is given by @component decorator. Also you can see different dependencies injection practice.

EDIT :: Example (Current angular 2/4 version)

import { Component } from '@angular/core';

@Component({
  selector: 'custom-component',
  templateUrl: './template.html',
  styleUrls: ['./style.scss'],
})
export class CustomComponent {}

In a nutshell, angular 2 is a component based MVC framework.

Klaassiek
  • 2,795
  • 1
  • 23
  • 41
binariedMe
  • 4,309
  • 1
  • 18
  • 34
16

The components and directives are the controllers, the template (HTML) processed by Angular and the browser is the view, and if you don't combine the model with the controller, you get a MVC pattern.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 2
    What I see in many examples on the web is people using transient services (separate instance per component) to define http methods to access data from the server, which is then stored directly in the view components. In this case you have no model layer at all, so to coordinate the display of multiple views you either have to have separate views talking directly to each other, or round tripping every interaction via the server. Trying to convince my team that this is not a suitable approach for a rich interaction SPA and that what we need is a proper model layer is proving impossible. – Neutrino Jul 06 '17 at 11:49
  • 6
    Services are the model is all. If you display the same data you just share a service with multiple components. Often it's a good idea to use observables to notify the views about changes in services. Some put business logic into components, other prefer to keep components dumb and have business logic in services. You can have multiple level of services (depending or where you provide them). You can have a service provided directly on a component (to get another instance for each component instance), or a parent component or singleton like services by providing them in `@NgModule()` – Günter Zöchbauer Jul 06 '17 at 11:53
7

Angular 2 is internally followed MVC because it's component follow complete MVC architecture.

But if you talk about what we do in angular2 is MV*. It can be MVVM or MVC. It's not fully MVC.

https://namitamalik.github.io/MVC-and-MVVM-with-AngularJS/

Arti Singh
  • 906
  • 1
  • 6
  • 21