In Angular, a Controller is defined by a JavaScript constructor function that is used to augment the
Angular Scope.
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.
If the controller has been attached using the
controller as syntax then the controller instance will
be assigned to a property on the new scope.
Use controllers to:
- Set up the initial state of the
$scopeobject. - Add behavior to the
$scopeobject.
Do not use controllers to:
- 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).
Setting up the initial state of a $scope object
Typically, when you create an application you need to set up the initial state for the Angular
$scope. You set up the initial state of a scope by attaching properties to the $scope object.
The properties contain the view model (the model that will be presented by the view). All the
$scope properties will be available to the template at the point in the DOM where the Controller
is registered.
The following example demonstrates creating a
GreetingController, which attaches a greeting
property containing the string 'Hola!' to the $scope:var myApp = angular.module('myApp',[]);
myApp.controller('GreetingController', ['$scope', function($scope) {
$scope.greeting = 'Hola!';
}]);
We create an Angular Module,
myApp, for our application. Then we add the controller's
constructor function to the module using the .controller() method. This keeps the controller's
constructor function out of the global scope.
We attach our controller to the DOM using the
ng-controller directive. The greeting property can
now be data-bound to the template:<div ng-controller="GreetingController">
{{ greeting }}
</div>
Adding Behavior to a Scope Object
In order to react to events or execute computation in the view we must provide behavior to the
scope. We add behavior to the scope by attaching methods to the
$scope object. These methods are
then available to be called from the template/view.
The following example uses a Controller to add a method, which doubles a number, to the scope:
var myApp = angular.module('myApp',[]);
myApp.controller('DoubleController', ['$scope', function($scope) {
$scope.double = function(value) { return value * 2; };
}]);
Once the Controller has been attached to the DOM, the
double method can be invoked in an Angular
expression in the template:<div ng-controller="DoubleController">
Two times <input ng-model="num"> equals {{ double(num) }}
</div>
No comments:
Post a Comment