- Define and describe module, controller and $scope
- Set values on $scope to see them updated in the view
- Define and describe $rootScope
- Explain the relation between DOM hierarchy and scope hierarchy
A module is a container for the different parts of your app – controllers, services, filters, directives, etc.
Most applications have a main method that instantiates and wires together the different parts of the application.
Angular apps don't have a main method. Instead modules declaratively specify how an application should be bootstrapped.
<div ng-app="myApp">
<div ng-controller="GreetingController">
<input type="text" name="name" ng-bind="name">
Hello, {{ name }}
</div>
</div>
angular.module('myApp', [])
.controller('GreetingController', function($scope) {
$scope.name = 'CJ';
});
- The reference to myApp module. This is what bootstraps the app using your module:
<div ng-app="myApp">
...
</div>
- The Module API:
- The empty array in angular.module('myApp', []). This array is the list of modules myApp depends on.
// Create a module with the name 'appName' and no dependencies.
angular.module('myApp', [])
// Get a module with the name 'appName'
angular.module('myApp')
- An Angular Controller is a JavaScript function.
- Controllers are defined/added on a module.
- Controllers add properties and methods to $scope which can then be used in the view.
- A Controller shouldn't try to do too much. It should contain only the business logic needed for a single view.
angular.module('myApp', [])
.controller('GreetingController', GreetingController);
function GreetingController($scope) {
$scope.name = 'CJ';
}
<div ng-app="myApp">
<div ng-controller="GreetingController">
<input type="text" name="name" ng-bind="name">
Hello, {{ name }}
</div>
</div>
- When a Controller is attached to the DOM via the
ng-controller
directive, Angular will instantiate a new Controller object, using the specified Controller's constructor function. - A new child scope will be created and made available as an injectable parameter to the Controller's constructor function as $scope.
In general, a Controller shouldn't try to do too much. It should contain only the business logic needed for a single view.
The most common way to keep Controllers slim is by encapsulating work that doesn't belong to controllers into services and then using these services in Controllers via dependency injection.
- Set up the initial state of the $scope object.
- Add behavior to the $scope object.
- Manipulate DOM — Controllers should contain only business logic. Putting any presentation logic into Controllers significantly affects its testability. Angular has databinding for most cases and directives to encapsulate manual DOM manipulation.
- Format input — Use angular form controls instead.
- Filter output — Use angular filters instead.
- Share code or state across controllers — Use angular services instead.
- Manage the life-cycle of other components (for example, to create service instances).
$scope
is an object used for data binding that we can define methods and properties on. It is automatically injected into our controllers so that we can use it.$scope
is just a JavaScript object.- Both the controller and the view have access to $scope
- "the glue between the controller and the view".
- The execution context for expressions
- "the glue between the controller and the view".
- Arranged in hierarchical structure which mimic the DOM structure of the application
angular.module('myApp', [])
.controller('AwesomeController', function($scope){
$scope.title = "I'm Awesome!";
});
angular.module('myApp', [])
.controller('AwesomeController', function($scope){
const vm = this;
// DON'T ACTUALLY WRITE THIS LINE OF CODE
// it's what happens when you set the controllerAs property on the component options
// .component('myComponent', {
// controller: function(){},
// templateUrl: 'template.html',
// controllerAs: '$ctrl'
// });
$scope.$ctrl = vm;
vm.title = "I'm Awesome!";
});
- Every Angular application has a single root scope.
- All other scopes descend from
$rootScope
$rootScope
can be injected into a controller just like $scope
angular.module('myApp', [])
.controller('AwesomeController', function($scope, $rootScope){
//now have access to $rootScope
});
- All scopes are created with prototypal inheritance
- All scopes have access to their parent scopes
- Whenever Angular cannot find a method or property on the local scope, it will check the parent scope, if it can't find it there, it will continue up the tree until it reaches $rootScope
- Define and describe module, controller and $scope
- Set values on $scope to see them updated in the view
- Define and describe $rootScope
- Explain the relation between DOM hierarchy and scope hierarchy