Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Controllers

fredycaceres edited this page Nov 7, 2016 · 2 revisions

controllerAs Controller Syntax

  • Use the controllerAs syntax over the classic controller with $scope syntax.

  • The controllerAs syntax uses this inside controllers which gets bound to $scope

    Why?: controllerAs is syntactic sugar over $scope. You can still bind to the View and still access $scope methods.

    Why?: Helps avoid the temptation of using $scope methods inside a controller when it may otherwise be better to avoid them or move the method to a factory, and reference them from the controller. Consider using $scope in a controller only when needed. For example when publishing and subscribing events using $emit, $broadcast, or $on.

    /* avoid */
    function CustomerController($scope) {
        $scope.name = {};
        $scope.sendMessage = function() { };
    }
    /* recommended - but see next section */
    function CustomerController() {
        this.name = {};
        this.sendMessage = function() { };
    }

Clone this wiki locally