Skip to content

Instantly share code, notes, and snippets.

@flipjs
Last active August 29, 2015 14:06
Show Gist options
  • Save flipjs/dbb0b136deecbdc7f4e3 to your computer and use it in GitHub Desktop.
Save flipjs/dbb0b136deecbdc7f4e3 to your computer and use it in GitHub Desktop.
$scope.ctrl = this OR controllerAs?
// 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