18.05.2016 Views

Mittwoch, 18. Mai, 2016

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Finishing Angular TODO application and deploying to production 104<br />

Views<br />

Simply put<br />

AngularJS views are HTML templates rendered by the AngularJS compiler to produce<br />

a manipulated DOM on your page.<br />

Controllers & $scope<br />

You can think of $scope as the bond between the view and the controller. Using a scope object<br />

($scope), the controller can manipulate the model, which in turn then automatically propagates<br />

these changes to the view and vice versa. Differently put, scopes provide a single source of truth¹⁷⁹<br />

within your application - no matter where you display some data in your view layer you should only<br />

have to change it in one place (a scope property), and the change should automatically propagate<br />

throughout the view.<br />

Controller instances are usually created when you use the ng-controller directive on an element<br />

like this:<br />

1 <br />

Important to note is that scopes can inherit the model from their parent scope, where the $rootScope<br />

is the highest in the “hierarchical chain” of scopes. More on $rootScope here¹⁸⁰.<br />

The controllers in AngularJS are defined like in the following example:<br />

1 angular.module('example').controller('MyController', ['$scope',<br />

2 function($scope) {<br />

3 $scope.someMessage = "Hi MEAN people! ;)";<br />

4 }<br />

5 ]);<br />

The reason why the above code wasn’t written like the following example (although working):<br />

1 angular.module('example').controller('MyController', function($scope) {<br />

2 $scope.someMessage = "Hi MEAN people! ;)";<br />

3 });<br />

¹⁷⁹http://en.wikipedia.org/wiki/Single_Source_of_Truth<br />

¹⁸⁰https://docs.angularjs.org/guide/scope

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!