JavaScript Why is concept of Model and Controller intermingled in Angular.JS? concept of model view controller

I am very new to Angular world and I have mostly worked with .NET MVC architecture where there is a clear delineation between entities like Model and Controller.

It is as clear as following in my opinion:

  • A Model is container of data
  • A controller is basically collection of the functions which handle the interaction between business and View on the client side

They are two separate files and two separate types. There is no way someone can confuse between a asp.net view and asp.net Controller

However, in Angular world(which is apparently a MVC architecture), a controller looks like something described at following link:

https://scotch.io/tutorials/making-skinny-angularjs-controllers

To me, by .NET standards, this is a hybrid of Model and Controller because it not only carries data (like model) but also carries functions**(Just like Controller)**. Not only that, model is actually part of Controller.

My question is that if a model in Angular looks like the one described in the link above, what does a model look like by itself?

Answer:1

Model

In AngularJS, the term model refers to the data stored inside the scope to using be used inside view. It can be the hard-coded value/object or the response received on the service call stored inside scope.

enter image description here

The above image gives a clear picture of how model is used on on the view. AngularJS also supports 2-way data binding. i.e. updating view (which is bound to a specific model) also updates the model value.

Controller

The purpose of controllers is to expose variables and functionality to expressions and directives.

enter image description here

As shown above, a controller is associated with a scope and all the models defined inside scope can be accessed on the view associated with the respective controller. Controller can be used to define business logic and handling events etc.

Answer:2

There is a separation between the model ($scope) and the view (HTML).

Yes, there are methods on $scope that are called from the view, but these are essentially controller actions - they are executed from the controller function's closure.

 app.controller('ctrl', function($scope) {
       $scope.data = 'hello';
       $scope.onclick = function(arg) {
             $scope.data = 'hello world!';
        }
 });

Microsoft ASPNET MVC is strikingly similar to Angular MVC:

 MVC Controller = ngController
 ViewData/ViewBag = $scope
 Controller DI = Angular DI in controller function
 Razor View Engine = ngRepeat, ngShow, ngModel, etc
 RenderActions = Data-driven service-injected directives
 RenderPartials = Read-only directives (data retrieved by another controller)
 Click triggers controller action = ngClick triggers $scope handler in controller's function closure
 Model Data Binding = AngularJS Data Binding
 ModelState validation = ngModel validation
 HTTP handlers/modules = HTTP interceptors

It makes me wonder if ASPNET MVC inspired Angular...

Note: ViewData and $scope are similar, but of course, $scopes resolve $scope properties through $scope inheritance, but in ASPNET MVC, child ViewData do not inherit from parent VIewData. Hmm... Microsoft, that would be a useful feature:)

Answer:3

var variableOne; var variableTwo; var variableThree; function veryInterestingFunction() { var variableFour; if(a){ variableFour = 'Good'; } else { variableFour = 'Decent'; } ...

var variableOne; var variableTwo; var variableThree; function veryInterestingFunction() { var variableFour; if(a){ variableFour = 'Good'; } else { variableFour = 'Decent'; } ...

  1. avoid declaring public instance variables

I'm having a table where the user will enter data in the fields when they enter each value in each cell, the last cell needs to be updated with its total. with this snippet I'm getting 45+90+36. I ...

I'm having a table where the user will enter data in the fields when they enter each value in each cell, the last cell needs to be updated with its total. with this snippet I'm getting 45+90+36. I ...

  1. angularjs get table values

I'd like to reduce my array of objects using lodash. My object: [ {date: '01/01/2016', price: 5}, {date: '01/01/2016', price: 10}, {date: '01/02/2016', price: 500} ] After the reduce: [...

I'd like to reduce my array of objects using lodash. My object: [ {date: '01/01/2016', price: 5}, {date: '01/01/2016', price: 10}, {date: '01/02/2016', price: 500} ] After the reduce: [...

  1. reduce array with one element
  2. reduce array with objects
  3. array_reduce with keys
  4. reduce with array
  5. array reduce with index
  6. array reduce with initial value
  7. reduce with array javascript
  8. array reduce with promise
  9. array reduce with condition
  10. array reduce with strings
  11. array reduce with keys php
  12. reduce with array ruby
  13. array reduce with examples
  14. reduce array of objects javascript
  15. reduce array of objects js
  16. reduce array of objects into one object
  17. reduce array to unique values
  18. reduce array to string javascript
  19. reduce array by adding elements
  20. reduce array of booleans

I'm new to web crawling. I am trying to crawl a webpage using java and I encounter a problem. I need to get the link in a 'HTML Tag' whose href is a javascript function. I have no idea how to get the ...

I'm new to web crawling. I am trying to crawl a webpage using java and I encounter a problem. I need to get the link in a 'HTML Tag' whose href is a javascript function. I have no idea how to get the ...