1

From here

In general, a Controller shouldn't try to do too much. It should contain only the business logic needed for a single view.

I can't understand why it's controller. It must be model. Please,explain.

EDIT:
This is the example from official developer guider:

angular.module('invoice1', [])
.controller('InvoiceController', function() {
  this.qty = 1;
  this.cost = 2;
  this.inCurr = 'EUR';
  this.currencies = ['USD', 'EUR', 'CNY'];
});

<div ng-app="invoice1" ng-controller="InvoiceController as invoice">
  <b>Invoice:</b>
  <div>
    Quantity: <input type="number" min="0" ng-model="invoice.qty" required >
  </div>
  <div>
    Costs: <input type="number" min="0" ng-model="invoice.cost" required >
    <select ng-model="invoice.inCurr">
      <option ng-repeat="c in invoice.currencies">{{c}}</option>
    </select>
  </div>

What I don't understand is that controller = model here. Am I not right?

  • @Yoshi I totally agree with you. But such questions are too. –  Dec 11 '14 at 13:36
  • 1
    It was a fair question because the answer to yours seems self-intuitive, making it unclear why you asked. – isherwood Dec 11 '14 at 13:37
  • @PashaTurok Plalx only helped you for your question. – Naeem Shaikh Dec 11 '14 at 13:38
  • @Naeem Shaikh See the edit,please –  Dec 11 '14 at 13:41
  • 4
    @PashaTurok The example you posted, though from the official guide, is a bit unfortunate as it only shows the controller as a *data-container*. Which makes it look like the actual model. With the example given think of `qty`, `cost`, `inCurr` and `currencies` as individual models, instead of all as one. There is even an argument to use `this.viewModel` inside the controller, which would make this a lot clearer. – Yoshi Dec 11 '14 at 13:44
  • 1
    Now your question is more clear, but it might better be phrased as "Why does this controller contain model data?" It's probably a reasonable question considering that the example controller isn't strictly following MVC principles. – isherwood Dec 11 '14 at 13:45
  • @isherwood I asked that way as I'm newbie in angular and just trying to understand it. Besides, I've read one more book - angular in 60 minutes the same pattern. –  Dec 11 '14 at 13:48
  • @NimChimpsky I think you are talking about ADM not RDM. I mean http://www.martinfowler.com/bliki/AnemicDomainModel.html –  Dec 11 '14 at 13:58
  • @NimChimpsky I totally agree with you - the things are different. But why should controller do business logic if we have model. What about loose coupling? –  Dec 11 '14 at 14:00
  • becuase the model just contains data, logic goes in service classes or controller. The example does look a bit weird, the convention is to use $scope.model, but it doesn't break the patter – NimChimpsky Dec 11 '14 at 14:01
  • @NimChimpsky Do you mean if I have BicyleModel than controller must do run? –  Dec 11 '14 at 14:02
  • @PashaTurok yes exactly, I believe that is the angular convention, which is different to domain driven desgin. – NimChimpsky Dec 11 '14 at 14:03
  • @NimChimpsky I can't agree with you. I've read a lot of classic books about OOP and everywhere the authors say - if an object can do its behaviour itself it MUST do its behaviour itself. And I totally agree. –  Dec 11 '14 at 14:05
  • @PashaTurok its not me your disagreeing with, its the pattern of MVC/angualr convetion – NimChimpsky Dec 11 '14 at 14:06
  • And please, upvote my question. It's quite unfair to downvote it. –  Dec 11 '14 at 14:06
  • possible duplicate of [Where to put model data and behaviour?](http://stackoverflow.com/questions/11112608/where-to-put-model-data-and-behaviour) – NimChimpsky Dec 11 '14 at 14:08
  • @plalx from http://stackoverflow.com/questions/11112608/where-to-put-model-data-and-behaviour "In the documentation and tutorials all model data is put into the controller scope" –  Dec 11 '14 at 14:13
  • @NimChimpsky Thank you for link. See the second sentence of the question. –  Dec 11 '14 at 14:14
  • @PashaTurok.. +1.. your code now make the question clear.. but you need to understand this is only javascript. and javascript doesnt work exactly how java works. models can be defined on the scope of controllers within the controller itself.. – Naeem Shaikh Dec 11 '14 at 14:26
  • @Naeem Shaikh Thank you. I think the problem is well described by the user of "possible duplicate". I also read all the examples from official tutorial. And I could not undestand - why what is controller does the model duties. I agree with that user that they are just bad examples. –  Dec 11 '14 at 14:29
  • @Yoshi I think it will be interesting for you. The possible duplicate was found and the user had the same problem with understanding. –  Dec 11 '14 at 14:33

3 Answers3

4

please understand controller does the business logic only.

MVC-model views and controllers.

model:only the data

view:View is display of model that is your data.  only the visible part(html)

controller: which handles and manipulates the data.

Model is data, not the business logic and controller handles it. also read this

Community
  • 1
  • 1
Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88
3

Model usually contains data and methods directly connected with that data. Controller connects that data with view (and services etc. in Angular's case). What exactly Angular's controllers do. It is common practice when people put a lot of logic to models or controllers. In Angular way there is a lot of other stuff that contain the logic. Models contain just the data, controllers using just to connect all that parts together. Look closely at ng-model directive: it's actually bind to view just a variable!

[Added later, after example added to question] He-he! That's tricky example. In it controller actually looks like a model. Look better at this example

phonecatApp.controller('PhoneListCtrl', function ($scope) {
  $scope.phones = [
    {'name': 'Nexus S',
     'snippet': 'Fast just got faster with Nexus S.',
     'age': 1},
    {'name': 'Motorola XOOM™ with Wi-Fi',
     'snippet': 'The Next, Next Generation tablet.',
     'age': 2},
    {'name': 'MOTOROLA XOOM™',
     'snippet': 'The Next, Next Generation tablet.',
     'age': 3}
  ];

  $scope.orderProp = 'age';
});

Here controller is controller, phones and orderProp are models and $scope is scope. =) Look at this picture: enter image description here You can see there models are 'inside' the controller's scope.

MVC is actually just pattern. You are free to use it in different mysterious ways, there are no compiller errors about wrong pattern usage. But the only reason to use it - to make stuff more straightforward. So less tricky ways are better!

Roman Bekkiev
  • 3,010
  • 1
  • 24
  • 31
3

Angular does not strictly follow the MVC. It is closer to MVVM: Model-View-ViewModel (or Model-View-Whatever, not that it is very helpful though).

In a nutshell:

  • Angular Controller and Scope go into the ViewModel layer:

The ViewModel can be considered a specialized Controller that acts as a data converter. (From here).

Its responsibility is to represent only the data needed for the View and glue them to the View. The data here are typically copied or referenced from the actual Model layer.

  • The Model layer (in MVVM) knows nothing about the View. This is where Angular Services go, and where you want to put the most of your business logic, to keep the Angular Controller (i.e. the ViewModel layer) thin.

A typical violation of this pattern, sadly frequently occuring in various tutorials, is to put server requests into Controller. Imagine you want to change your backend - now you are rewriting your Controller! Every single one that talks to your backend! Instead your Controllers should not know anything about the server. That information goes into your Model (Angular Services). Then with any change of the backend, you only need to rewrite the Services specifically dealing with it. Much cleaner and easier to maintain.

Community
  • 1
  • 1
Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110