用于ng-view的"Controller as"语法

use*_*761 29 angularjs ngroute

我想知道我如何Controller as结合使用语法,ngRoute因为我无法做到ng-controller="Controller as ctrl"

rob*_*rob 45

controller as$routeProvider配置中指定控制器时,可以使用语法.

例如

$routeProvider
    .when('/somePath', {
        template: htmlTemplate,
        controller: 'myController as ctrl'
    });
Run Code Online (Sandbox Code Playgroud)


小智 39

或者,您可以指定控制器分配,就像使用时创建新指令一样controllerAs.

    $routeProvider
        .when('/products', {
            templateUrl: 'partials/products.html',
            controller: 'ProductsController',
            controllerAs: 'products'
        });
Run Code Online (Sandbox Code Playgroud)

  • 我认为你的方法更清洁了 (7认同)