Last active
August 29, 2015 14:06
-
-
Save flipjs/dbb0b136deecbdc7f4e3 to your computer and use it in GitHub Desktop.
$scope.ctrl = this OR controllerAs?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // OLD STYLE OF CONTROLLER CONSTRUCTOR | |
| ;(function() { | |
| angular.module('oldstyle', []) | |
| .controller('MainController', MainCtrl) | |
| function MainCtrl($scope) { | |
| $scope.MainCtrl = this | |
| this.users = {} | |
| this.userList = [] | |
| } | |
| MainCtrl.$inject = ['$scope'] | |
| })() | |
| // NEW WAY OF DOING CONSTRUCTOR, using controllerAs | |
| // $scope is not explicitly referenced here | |
| // in html: <div ng-controller="MainControllerAs as ctrl"></div> | |
| ;(function() { | |
| angular.module('newstyle', []) | |
| .controller('MainControllerAs', MainCtrlAs) | |
| function MainCtrlAs() { | |
| this.users = {} | |
| this.userList = [] | |
| } | |
| })() | |
| // Before controllerAs, this is how we do it, assigning to $scope | |
| ;(function() { | |
| angular.module('newstyle', []) | |
| .controller('MainControllerScope', MainCtrlScope) | |
| function MainCtrlScope($scope) { | |
| // assign properties to $scope | |
| $scope.users = {} | |
| $scope.userList = [] | |
| } | |
| MainCtrlScope.$inject = ['$scope'] | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment