Skip to content

Instantly share code, notes, and snippets.

@chinghanho
Created October 3, 2013 09:12
Show Gist options
  • Save chinghanho/6807343 to your computer and use it in GitHub Desktop.
Save chinghanho/6807343 to your computer and use it in GitHub Desktop.
Angular controller this vs $scope.
'use strict';
var app = angular.module('app', []);
app.controller('AppCtrl', function ($scope) {
this.foo = 'bar';
$scope.hello = 'world';
});
app.directive('app', function () {
return {
restrict: 'C',
controller: 'AppCtrl as app'
}
});
angular.bootstrap(document.getElementById('container'), ['app']);
<div id="container" class="app">
<input type="text" ng-model="hello">
<p>{{hello}}</p> <!-- world -->
<input type="text" ng-model="app.foo">
<p>{{app.foo}}</p> <!-- bar -->
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment