Using ngRoute with 'controller as' syntax

2019-09-12 08:43发布

I have the following config and controllers

.config(function($routeProvider){

        $routeProvider

        .when('/', {
            templateUrl: 'page-home.html',
            controller: 'homeController',
            controllerAs: 'hctrl'
        })

        .when('/about', {
            templateUrl: 'page-about.html',
            controller: 'aboutController',
            controllerAs: 'actrl'
        })

        .when('/contact', {
            templateUrl: 'page-contact.html',
            controller: 'contactController',
            controllerAs: 'cctrl'
        });
    })

    .controller('homeController', function(){
        this.pageClass = 'page-home'
    })

    .controller('aboutController', function(){
        this.pageClass = 'page-about'
    })

    .controller('contactController', function(){
        this.pageClass = 'page-contact'
    });

My problem comes when I use in in the index.html.

 <div class="page {{pageClass}}" ng-view></div>

Since I'm not using $scope, just writing {{pageClass}} won't work. How can I get around this using the controller as syntax?

Edit

I got a couple of good answers. I also discovered an alternate way to do this if you want to name your controllerAs values with different names: hctrl, actor and ctrl (like my code above):

You could do this in the html:

<div class="page {{hctrl.pageClass || actrl.pageClass || cctrl.pageClass}}" ng-view></div>

3条回答
Melony?
2楼-- · 2019-09-12 09:07

Whatever you wrote in the controllerAs value need to be prepended to the variable, like {{actrl.pageClass}}

查看更多
狗以群分
3楼-- · 2019-09-12 09:25

A good approach towards this problem is by setting the pageClass as a configuration in the routes definition and then create a directive that gets these definitions to be applied as whatever you want them to be (of course within the scope where the directive is applied to).

DEMO

Javascript

Define your route configuration with data key-value object.

.config(function($routeProvider) {

    $routeProvider

        .when('/', {
            templateUrl: 'page-home.html',
            controller: 'homeController',
            controllerAs: 'hctrl',
            data: {
              pageClass: 'page-home'
            }
        })

        .when('/about', {
            templateUrl: 'page-about.html',
            controller: 'aboutController',
            controllerAs: 'actrl',
            data: {
              pageClass: 'page-about'
            }
        })

        .when('/contact', {
            templateUrl: 'page-contact.html',
            controller: 'contactController',
            controllerAs: 'cctrl',
            data: {
              pageClass: 'page-contact'
            }
        });

  })

Create a directive that sets these data with the directive's controller.

  .directive('routeData', function() {
    return {
      controller: 'RouteDataController',
      controllerAs: 'RouteData',
      bindToController: true
    }
  })

  .controller('RouteDataController', function($rootScope, $route) {

    var self = this;

    $rootScope.$on('$routeChangeSuccess', setCurrentRouteData);

    setCurrentRouteData();

    function setCurrentRouteData() {
      angular.extend(self, $route.current.$$route.data || {});
    }

  })

In your index.html apply the directive itself and access the directive's controller to get the data values.

<div ng-view route-data class="page {{ RouteData.pageClass }}"></div>
查看更多
戒情不戒烟
4楼-- · 2019-09-12 09:33

Specify the controller as name

<div class="page {{hctrl.pageClass}}" ng-view></div>
查看更多
登录 后发表回答