Read more

"controller as" syntax for AngularJS 1.2+

Thomas Klemm
December 06, 2014Software engineer

In the Controller:

// Instead of doing this:
app.controller('TodoCtrl', function ($scope) {
  $scope.input = 'ex. buy milk';
});

// Do this:
app.controller('TodoCtrl', function () {
  this.input = 'ex. buy milk';
});
Illustration online protection

Rails professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
Read more Show archive.org snapshot

In the HTML:

<!-- Instead of this: -->
<div ng-controller="TodoCtrl">
  <input type="text" ng-model="input" />
</div>

<!-- Do this: --> 
<div ng-controller="TodoCtrl as todo">
  <input type="text" ng-model="todo.input" />
</div>
Posted by Thomas Klemm to makandra dev (2014-12-06 11:45)