Friday, 27 May 2016

Controllers in Anjularjs

AnjularJS controllers :
         As per SOLID principal as well MVC suggested way is separation of conterns(Model,View,Controller) we have to prepare object/model is job of controller, we can construct/prepare model itself at controller but it is violation mvc pattern.

Model(employee)---------$scope-------View
Scripts.js------------------anjularjs------demo.html              
   


Constructing a complex object/employee in controller and attaching to $scope object. $scope is a glue between model object and view.
    
 If we define a controller as a sperated js file or same file $scope is mandatory to access complex object (employee) object.



Notes :
       1. In anjularjs if we missspelled the controller name then anjularjs will report the error.
as like
Browser : Developer tolls--console 

 Error :  Argument "mycontroller1" is not a function, got undefined.

to get good understandable error description use anjular.js.(not minified version of anjular.js)

      2. missspelled the property of object like

   {{ employee.gende} instead of  {{ employee.gender}}----anjular not report any error.
simply it will ignore the gender value.



1 comment:

  1. The differences are that, first, in the ng-controller directive we not only pass the name of the registered controller, but also, a variable name for an instance of the controller; vm stands for ViewModel but a more meaningful name can be used and we’ll talk about it later. Second, when we use double curly braces or the ng-model directive, we use vm.myModel to access to the variable defined in the controller. Read more: AngularJS Training and Tutorial

    ReplyDelete