1

I'm doing an online course on AngularJS and they suggest to write controllers like this:

Controller:

app.controller("TestController", function() { this.printable = "Hello, World"; }); 

View:

<div ng-controller="TestController as test"> <h1>{{ test.printable }}</h1> </div> 

Which is odd, as every other tutorial I've read suggests to write the controller as:

app.controller("TestController", ["$scope", function($scope) { $scope.printable = "Hello, World"; }); 

What's the difference between the two approaches?

3
  • it would be usefull to name the course you are following so we know more about the context. Commented May 21, 2014 at 15:09
  • Thanks @ErikKronberg - that's much better than the other answers I could find! Commented May 21, 2014 at 15:12
  • @RGraham yeah it's a great answer! Commented May 21, 2014 at 15:13

1 Answer 1

1

The difference is the scope of the object you're interacting with. $scope is an object injected into your controller. Your controller is an object representative of a given piece of your angular application.

Earlier on in Angular, controllers weren't objects. They are now.

You should be fine going either way (if you're concerned about which is correct), but controller as is a little more concise.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.