AngularJS’s Controller As and the vm Variable | John Papa

John Papa

Evangelist on the loose

AngularJS’s Controller As and the vm Variable


The great thing about coding is that we are able to mix coding style and personal/team preference together. This post is all about a preference of mine that has helped me stick to be principles of readable, efficient, and maintainable code with AngularJS.

It’s no secret that I am a fan of the “Controller As” syntax that was revealed in AngularJS 1.2.0, in fact I wrote an entire post about the Controller As feature. I’m also a fan of readable code and keeping things consistent and simple. We tend to spend a lot of time reading code. In fact, I bet you spend much more time reading code than you might imagine … more-so than writing it.

You’ll see this and more in my upcoming Pluralsight course AngularJS Patterns: Clean Code, due out this summer 2014.

vm in the HTML

When I create controllers these principles apply well to the Controller As syntax. This is where I like to apply the variable vm for my controllers. Here is some typical code I write in HTML

<div ng-controller="Shell as vm">
  <article ng-controller="Customers as vm">
    <ul ng-repeat="c in vm.customers">

Notice I name the controller’s instance variable vm in the HTML. In the MV* world, and specifically in MVVM patterns, VM represents the View’s Model (aka ViewModel). But wait, this is MVC right? Meh, it’s MV* .. I’m all for patterns, but I don’t get hung up on conforming for the sake of conforming. So when accessing the binding scope from the controller I consider it the glue between the HTML View and the JavaScript Controller. This is the View’s Model … to to me, I like calling it vm. And hey, vm is short, contextual, gets the point across, and is readable.

Can you use anything here? Sure. In fact many examples/demos may have Customers as customers or CustomersCtrl as customers. That works fine too, but I like the short Customers as vm as it tells me instantly that the controller is for Customers and now I know inside my HTML I can use vm.

Mainstream Controller As

So how would it look if I used something more mainstream? Let’s look.

<div ng-controller="Shell as shell">
  <article ng-controller="Customers as customers">
    <ul ng-repeat="c in customers.customers">

Again, this works fine, but now I have customers.customers in the ng-repeat. That’s ugly and it doesn’t convey that it represents controller dot array. Well, what if we call it Customers as custCtrl or Customers as customerCtrl? They work too … and it really comes down to personal preference (of which I am not a fan of these). That’s why I broke from the mainstream here.

vm in the JavaScript

Ah, so what does the JavaScript look like? Here I also use vm to represent the binding scope. This gets rid of the $scope variable from most of my controllers.

    .controller('Customers', [function() {
      var vm = this;
      vm.title = 'Customers';
      vm.customers = [
        {name: 'Haley'}, {name: 'Ella'}, {name: 'Landon'}, {name: 'John'}

Notice that I use var vm = this; and then I decorate vm with the members that should be exposed and data-bindable to to the View. This does 3 things for me.

  1. Provides a consistent and readable method of creating bindings in my controllers
  2. Removes any issues of dealing with this scoping or binding (i.e. closures in nested functions)
  3. Removes $scope from the controller unless I explicitly need it for something else
  4. Makes me happy since its short :)

The name vm here in the JavaScript does not have to match what is used in the View. So there is no coupling there other than that I like using vm in both.

One of my favorite reasons for using Controller As is that I no longer have $scope hanging around and the temptation to abuse it as a catch-all and do-everything $scope object are removed. For example, if $scope is always there as a dependency it is easier to add watches, apply, digest, on, emit, broadcast and more right in the controller. Now, they may be needed, but often I can find better ways by abstracting those to factories. So if the $scope is not present as a dependency, I am less tempted.

Nesting Controllers

One interesting aspect of this is when we nest controllers in a View. The HTML may look something like this

<div ng-controller="Shell as vm">
  <article ng-controller="Customers as vm">
    <h2>{{vm.title}} in {{$parent.vm.title}}</h2>
    <ul ng-repeat="c in vm.customers">

Notice here that the vm is used in both the Shell and Customers controller’s HTML elements. My rule of thumb is to use vm until it is no longer clear (borrowed from the great Ward Bell). This might be a case for that (up to your own preferences). Both controllers have a title property which may be difficult to clearly identify. It is possible as shown here using the $parent syntax, but if you feel this is a bit to much with the vm here in the HTML there are other options. One of these is to clarify the vm as shown below.

<div ng-controller="Shell as shellVm">
  <article ng-controller="Customers as customersVm">
    <h2>{{customersVm.title}} in {{shellVm.title}}</h2>
    <ul ng-repeat="c in customersVm.customers">

Now we see the shellVm and the customersVm in the HTML and it is clear if we want the title property on either how we can get to it.

And remember, the JavaScript doesn’t change one bit. We can still use var vm=this; in the Controller’s JavaScript, use this, or call it anything we want over there.

Choosing a Path

Really this is a team preference that you have to decide upon. As long as you are consistent, there are many ways to skin this cat. I prefer to use vm (until it is not clear) and in 95% of the cases for me it’s worked wonderfully clear. Choose what works for your team and enjoy coding!

tags: angular javascript
  • Ward Bell

    “Good stuff!” says the lesser Ward Bell

    • johnpapa7

      Ward – Wait, there are two of you? Uh oh.

  • Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1636()

  • markrendle

    I was also a user of the “same-name” approach (although in my case I used “ctrl”), until I started using UI-Router and working with several levels of nested views. Then I found that I wanted to declare members on a parent controller and bind to them in nested controllers, at which point having distinct names for each “ctrl” becomes much more useful. So now I default to using distinct names, because it saves refactoring later.

  • Sampath Lokuge

    Really nice approach John.I would like to maintain the 2nd approach which you have mentioned on Nesting control’s section.Which is more elegant and clear.That is ‘Customers as customersVm’ approach.And I would like to give a same vm name inside the Controller also.It’s like this ‘var customersVm =this;’.So What’s your thoughts about that simple extension ? :)

    • johnpapa7

      Sampath – Thanks. That works too. I prefer vm in the JavaScript since the context is already super clear personally. But that works just fine too.

      • Sampath Lokuge

        Thanks John.Keep up the good work. :)

  • yngvebn

    While I do agree that the ControllerAs syntax makes for pretty code, I’m not sure I quite agree with your ‘vm’-approach. In your Customer-controller example, the take-away for assigning var vm = this; is that you can get rid of a single .bind(this) on your function, and in my opinion I can live with that binder. var vm = this; reminds me too much about var self = this; in Knockout which I’m really glad I don’t have to write anymore! :)

    I’m thinking that if the controller gets full of functions that require you to call .bind(this), I’d consider moving some of that logic out into other services, to keep my controllers from not growing out of proportion, leaving me with very few functions requiring .bind(this) in my code.

    When it comes to the markup, you say you don’t like for instance “customers.customers” and would rather do “customersVm.customers”, whereas I’m thinking the namingissue is further down. What about renaming the customers-property and call it ‘all’ or something more verbose, so that the markup becomes “ng-repeat=’customer in customers.all'” instead. That enables us to be verbose and declarative in the markup, instead of having to postfix everything with ‘Vm’.

    I really like how you remove redundant “Controller” or “Ctrl” on your Controller-declarations, so why not do the same for variables in the view? We already know that the controller is responsible for providing the view with data and other functionality, so it’s implicit that it’s a “ViewModel” already.

    Just my humble opinions. What do you think?

    • johnpapa7

      yngvebn – Thanks for the feedback. As I said there are many ways to skin this cat. It really is up to you. I’ve stated my case on this vs vm in the JavaScript so I won;t repeat myself, but I will add that using “this” often leads to confusing code within closures. I agree that self=this is not something I prefer, but only because self is not as descriptive in a controller as vm is. I believe the mainstream demos for Controller As use “this” so if that’s your choice, you have good company. I’ve personally found it easy to teach and maintain when using `var vm = this;`

      You could use `customers.all` if the context works well for you; in this case it certainly could. The JavaScript would be `vm.all = [ … ];` Consistency and clarify/readability are key and I think this is fine.

      Yeah I like removing the redudnant Controller, but consider that its redundant because I say `ng-controller=’Customer as vm` …. I already said the word controller as its in a `ng-controller`. I do like having the `customerVm` or `vm` in the HTML so I am clear that its the `vm` and not a customer. Why? Because when I am in the HTML I am often writing code away from that directive perhaps in `{{}}` or `ng-repeat o in customerVm.orders` .

      However, while I am talking about my preferences here keep in mind your preferences for your team are what is important. Not mine. So if these conventions work for you, roll with it!

  • Angel

    So John, have you dumped Durandal?

  • Joe Hoppe

    I tried to share this on twitter, but I received this error message: Warning: mysql_connect(): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in D:homesitewwwrootwp-includeswp-db.php on line 1372

    • Sampath Lokuge

      Me too came that message.Not only from the TW but also from the others .So I tried 2,3 times and then worked.Try and try again,One day you’ll fly. :)

    • johnpapa7

      Thanks. Not sure what’s up. Notified the Azure folks to ask for help.

  • Gustavo Robles

    Nice article, do you know how can I use the parent vm in the children?

    Lets say I have a parent controller that has vm.product, and then in the html I have a nested controller but inside that particular html I need access to vm.product, any thoughts? I did some research and I found something about $scope.$ but, since vm gets rid of $scope, how can I achieve this?

    • johnpapa7


  • Keuller Magalhaes

    Hi John, I’d like use this approach because is more clear and simple, but I’d like to know if it works together ngRoute module ? For example, when I define some route like that:

    $routeProvider.when(‘/empresas’, {
    templateUrl: ‘/basico/empresas/view’,

    I’m specifying my template and controller, in this case I don’t have ng-controller directive, so I cannot define ‘controller as’ syntax.

    • johnpapa7

      use `controllerAs` in the route

  • Pingback: AngularJS's Controller As and the vm Variable |...()

  • Chief

    Nice write-up! We’ve actually used Angular Classy ( which helps a ton with controller readability. It also supports “Controller As!”

    • johnpapa7

      looks cool!

  • Pingback: Andrey on .NET | Интересности #40()

  • Brian

    This is great. I definitely like the clarity it brings when using controllers. Do you have any suggestions for isolated scope directives? It is common in our application to have a directive that could benefit from a single root ViewModel for the sake of it’s own template but where we restrict the input / output bindings of the directive to a few primitive types. Basically don’t want to provide the entire VM via a binding, but means we have to copy values and wire up $watch statements to keep properties on the VM in sync with specific bindings.

  • sephie

    I noticed that in you “controllers-with-or-without-sugar” article you used the following style in controllers:

    var vm = this;
    return vm; // <– look at meee!

    In the current article's 'Customers' controller you're omitting the return statement at the end. Why?

    One of the best explanations and discussion on the new syntaxt that I've read. Thanks!

    • johnpapa7

      the return vm was a typo. thanks for catching it. i fixed it in that post.

  • Pingback: accessing $scope from unit tests when using the vm “ControllerAs” syntax from AngularJS HotTowel()

  • Matjaz Jurecic

    how to use this in directives?

  • Matjaz Jurecic

    how to use this in directives?

  • Pingback: Angularjs Controller | Stellar Software Development()

  • Josh

    Hi John,

    How can we use $scope.$apply() with “controller as” syntax? I have two sections on my html page I’m swapping between them In ajax callback using a Boolean flag and ng-show. On stack overflow it says to use $scope.$apply() inside the ajax callback.
    vm.$apply throws function undefined error.


    • johnpapa7

      Josh – You can use $scope.$apply() in that case and just inject $scope for that purpose (still using vm for everything else). However, if you switch fro using ajax to using Angular’s $http, then you won’t need to call $apply as angular’s $http does that for you. That’s what I recommend

  • Erdem

    I use ui-route for routing, i try to use controllerAs syntax but i can not achive to use it efficiently with ui-route. I can see controller execution in console, but model(vm.Title i.e.) doesn’t bind in the page.

    Please could you send a working example with ui-route?

    • johnpapa7

      Make sure you set `controllerAs` in the route definition

    • ashish

      I think you can get it right by using something like this.

      controller: ‘SomeCtrl as some’. I know I am posting it late but this works.

  • Ivan

    First of all thank you for examples and style guides John. I have learned a lot reading your posts and watching videos. Great work!

    I have a question regarding controller as pattern. I am using this pattern almost everywhere. This almost is what I dont like. Exception is when I need to subscribe to event using pub-sub mechanism. Pub/sub mechanism is custom and it is placed in notificationService so to publish an event all you need is to inject it and call notificationService method. Lets say you want to publish that user has signed in all you need to do is call notificationService.userHasChanged method from security service. notificationService is using $rootScope to broadcast message in background. Problem is subscription. I have implemented method notificationService.onUserHasChanged but in order for it to work and subscription to destroy properly when controller $scope dies we have to pass subscriber $scope. Every controller in application has corresponding service for data ops and whatever but in this case I cant subscribe from corresponding service because I cant pass notification or data back to controller. I hope you understand my dilemma. Code from notificationService is bellow.


    // publish userHasChanged
    function userHasChanged() {

    // subscribe to userHasChanged event.
    function onUserHasChanged($scope, handler) {
    $scope.$on(CURRENT_USER_HAS_CHANGED, function (event, message) {

    Do you have any thoughts or examples on how would be possible to use pub sub with controller as pattern?

  • raqystyle

    Thanks for your article! From my point of view, Controller As syntax helps to avoid uncertainty while nesting controllers. Imagine you have a big view (in terms of amount of HTML-code) which use Bootstrap (lot’s of classes). In this case Controller As syntax allows you to make somebody’s (who comes after you) life easier by specifying from which scope does title go. However, with such a view simple and short vm does absolutely nothing. What’s why, I believe, Angular team introduced, sort to speak, aliases for controller that allows us to use controller’s method through the scope. I mean, somethingCtrl as smth === $scope.smth === this. Checkout this example:

  • sebastian concept >>>

    Actually I’d agree with this pattern but the author chosen a terrible name when he wrote var vm = this

    VM is too strongly tied to virtual machine to try to overload it with another convention. We all have IDEs with autocompleter so screw abbreviations that hides assumtions that overload your memory and expose intention by doing viewModel = this.

    • Greg

      Completely illogical. We’re talking about context here. If we were to follow this logic, and never use any variable name or abbreviation because it has been used elsewhere, code would be virtually unreadable. What next? We can’t use “o” because it means “Oxygen” in the periodic table?

    • johnpapa7

      vm is also very well known for ViewModel in the MV* world (MVVM). When writing code view model makes sense, while virtual machine doesnt.

      Feel free to write it out, too. (i wouldnt)

      var viewmodel = this;

  • ptgamr

    Hi John. I’m using controllerAs syntax for the parent directive, and I realize that the child directive template still can use the ‘vm’ variable. Could you explain that? Thanks!

  • Jose Alfonso

    I am using the controller as syntax extensively and I just run into a scoping issue when using ngRepeat and aliases to obtain the filter count

    I am basically using this trick as noted in

    List of people



    There is no result

    Number of filtered people: {{filteredPeople.length}}

    There is a scoping issue with this trick when using controller as syntax. If you are not injecting the $scope in your controller the filteredPeople var will not be updated. I’ve tried using vm.filteredPeople but then the results do not get updated by ngRepeat. Is this a bug perhaps in ngRepeat or am I missing something? I am using angular 1.3.15.


  • Mohamed Taraman


    I can access parent controller from child’s view

    But, how can I access a variable in Parent controller from Child Controller?

    I am using UI-Router

    controller: “ParentCtlr”,

    controllerAs: ‘Parent’,


    angular.module(‘app’).controller(‘ParentCtlr’, [function () {

    var vm = this;

    vm.title = ‘Customers';


    angular.module(‘app’).controller(‘ChildCtlr’, [function () {

    var vm = this;

    console.log(Parent.title); //doesn’t work


    • Ersen Başaran Şen

      Hello Mohamed,

      Could you found a solution for your question? I have the same problem.

      • Mohamed Taraman

        Hello Ersen

        Yes I found a solution


  • SocialChooozy

    Good idea, thanks for sharing

  • Claude Berube

    Hi John,

    How can I use controller as with components that require $scope such as ui-grid?

  • Joe Aliperti

    Do you do something similar for an Angular Service to avoid “this” name collisions from inside asynchronous closures? In the context of a service, though “view model” doesn’t really make sense. Do you use another name, something like “var self = this” inside a service?

    • johnpapa7

      I don’t usually have a this problem in services. But if you do and you are using ES5, then you can use self or any capture.

%d bloggers like this: