Ram Mudaliar Ram Mudaliar - 1 month ago 21
AngularJS Question

What does 'var vm = this;' mean in angular controllers?

I was browsing some stuff on github and I saw inside controller.js, someone had put the following:

function ImageController ($scope, $timeout) {
var vm = this;
}


What is this used for?

Answer

The vm in this case means viewmodel.

It is a shortcut, so instead of writing this.someMethod() you can use vm.someMethod().

Very common when you use Controller As syntax, so you donĀ“t use the $scope by "accident".

Also, the this keyword can be messy to use, since it may reference different things depending on where it is used.