Using "Controller as" Syntax in Angular Routes

I’ve been using the new “Controller as” syntax in Angular for a few months, but today I realized I didn’t know how to use the syntax for route controllers. After spending a few minutes scratching my head and “Googling with Bing”, I figured it out. The $routeProvider supports a “controllerAs” property for routes:

1
2
3
4
5
6
7
$routeProvider
    .when('/', {
        templateUrl: 'Views/Home/Index.html',
        controller: 'HomeCtrl',
        controllerAs: 'vm'
    })
    .otherwise({ redirectTo: '/' });

That’s it. Not very complicated, but hopefully it will save someone a few minutes of research.

I am now accepting new clients for part-time consulting and software development projects. Learn more

I haven't configured comments for this blog, but if you want to get in touch, you can find me on Twitter