"Controller as" syntax for ng-view
Asked Answered
R

2

30

I was wondering how I use the Controller as syntax in combination with ngRoute since I cant do ng-controller="Controller as ctrl"

Ryon answered 26/5, 2014 at 0:4 Comment(0)
M
46

You can use the controller as syntax when you specify your controller in the $routeProvider configuration.

e.g.

$routeProvider
    .when('/somePath', {
        template: htmlTemplate,
        controller: 'myController as ctrl'
    });
Multifid answered 26/5, 2014 at 0:16 Comment(0)
A
40

Or, you can specify a controller assigning like when you create a new directive using controllerAs.

    $routeProvider
        .when('/products', {
            templateUrl: 'partials/products.html',
            controller: 'ProductsController',
            controllerAs: 'products'
        });
Allen answered 30/11, 2014 at 14:35 Comment(2)
I think your approach is a lot cleanerOppidan
Exactly, when implementing, please prefer this solutionBulimia

© 2022 - 2024 — McMap. All rights reserved.