AngularJS : Controller As and the vm Variable

AngularJS : Controller As and the vm Variable

AngularJS : Controller As and the vm Variable

From AngularJS 1.2 onwards, the “Controller As” syntax was introduced.
This principle makes the controller code creation more readable.

Essentially we are assigning “this” to the variable “vm” and attaching properties and behaviour to this created “vm” object.

Of course we can use another name than “vm” in the “Controller As” syntax.
Under the hood, Angular creates the $scope object and attaches the properties and behaviour.
However by using the “Controller As” syntax, the code remains clean from controller level and only the alias name is visible on view level.

There are no direct advantages or disadvantages in using the “$scope” or the “Controller As” approach.
It’s just a matter of personal choice, however, using the controller as syntax makes the controller’s JavaScript code more readable.


About the Author

Daan is a Creative-Geek who loves learning and sharing new techniques! Follow him on Twitter to keep up to date with the Creative-Geeks blog and other subjects. Contact him on e-mail : info[at]creative-geeks.com.