Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Why use "Controller as vm"?

Tags:

angularjs

This entire weekend, I was in much distress, not understanding why a parent controller's function was not being recognized by a child controller.

I soon realized that having my controller as vm was the cause:

 <div data-ng-controller="ParentCtrl as vm">    <div data-ng-controller="Child1 as vm"></div>    <div data-ng-controller="Child2 as vm"></div>  </div> 

Sure, it all seems obvious now that neither child1 nor 2 will see functions in ParentCtrl, and if instead I had used the prior pattern of working without vm, and instead had $scope, all would be well.

So my question is "What does it benefit anyone for using the "vm" method, and if it is superior to not using it, how can one call function calls in the ParentCtrl short of emit?

Thank You

like image 796
Mike Jones Avatar asked Mar 09 '14 17:03

Mike Jones


People also ask

Why are controllers used in AngularJS?

AngularJS application mainly relies on controllers to control the flow of data in the application. A controller is defined using ng-controller directive. A controller is a JavaScript object that contains attributes/properties, and functions.

What is VM in AngularJS?

Instead of the $scope variable provided by Angular we simply declare a vm variable (which stands for view model) and assign this to it (i.e. the instance of the controller function). All variables will now be assigned to the vm object instead of $scope.

What is controller in AngularJS?

AngularJS Controllers AngularJS applications are controlled by controllers. The ng-controller directive defines the application controller. A controller is a JavaScript Object, created by a standard JavaScript object constructor.

What is the use of angular controllers in the application?

The controller in AngularJS is a JavaScript function that maintains the application data and behavior using $scope object. You can attach properties and methods to the $scope object inside a controller function, which in turn will add/update the data and attach behaviours to HTML elements.


1 Answers

One advantage of using the controller as syntax is that it allows you to define your controllers as a simple javascript constructor function with properties and functions exposed directly from the instantiated object rather than the $scope.

For example:

function MyController() {      var ctl = this;      ctl.message = 'You have not clicked anything yet.';      ctl.onClick = function() {         ctl.message = 'You clicked something.';     };      return ctl; }  ...  myModule.controller('MyController', MyController);  ...  <div ng-controller="MyController as vm">     <span>{{vm.message}}</span>     <button ng-click="vm.onClick()">Click Me</button> </div> 

Notice that we are able to use a controller that is plain old javascript without even being tied to angular. For scenarios where you need additional dependencies such as $scope or other services, you can still easily pass them in to your constructor, but this pattern encourages less clutter directly on your $scope and also solves the problem of variable hiding when variables are set directly on the scope.

Ultimately it really comes down to a matter of preference, but for me I really like not having to define everything directly on the scope and to treat my controllers as any old javascript object as much as possible.

Here's an excellent article on the usage of controller as: http://www.johnpapa.net/angularjss-controller-as-and-the-vm-variable/

like image 60
bingles Avatar answered Sep 19 '22 14:09

bingles