Controllers in Angular
ng-controller
AngularJS controllers control the data of AngularJS applications. In other words, AngularJS applications are controlled by controllers. The ng-controller directive defines the application controller. It is created by a standard JavaScript object constructor. We can add methods to our controller.
Syntax:-
< element ng-controller="expression" >< /element >
Further Explanation:-
Value | Description |
---|---|
expression | The name of the controller. |
Example Explanation:-
The AngularJS application is defined by ng-app="angular_app". The application runs inside the < div >.
- The ng-controller="person_controller" attribute is an AngularJS directive. It defines a controller.
- The person_controller function is a JavaScript function.
- AngularJS will invoke the controller with a $scope object.
- The controller creates two properties (variables) in the scope (first_nameand last_name).
- The ng-model directives bind the input fields to the controller properties (firstName and lastName).
- Finally, we call fullname() to get the full name.