![]() |
VOOZH | about |
AngularJS applications depend on a controller to control the flow of data through an AngularJS application. AngularJS architecture uses the MVC model i.e the Model View Controller. The model is responsible for maintaining the application data, the View for displaying data or some part of data to the user, and the controller for communication between the model and the view. A Controller is a JavaScript object that consists of various attributes, properties, and functions. Properties can be added to the controller using the $scope object. The $scope object acts as a glue between the controller and view. It simply acts as the glue between the model and the view. They take input from the view, process it, and return the output to be displayed to the user.
The $controller service is used to instantiate a controller. It is a call to the $injector but extracted into a service.
Syntax:
$controller(constructor, locals);
Parameter Values: The $controller takes 2 arguments:
Return Type: It returns an object that is an instance for the given controller.
Approach: The $controller can be implemented with the following approach:
<div ng-app = "myApp" ng-controller = "myCtrl">
var app = angular.module
app.controller("myCtrl",function($scope)){}Example 1: This example shows how to create a controller and use its properties in an HTML element.
Explanation: In the above example, we have created a module named "myApp". We have attached a controller named "myCtrl" to the module using the JavaScript constructor and have passed $scope as a parameter. We have attached properties like string, object, and array to the controller using the $scope object. We have used these properties in the <div> tag where we have specified the controller using the ng-controller directive.
Output:
Example 2: In this example, we will be creating an external file for the controller.
In larger applications, controllers are stored in external files. To implement this, from the above example you just need to put the code inside the <script> tag where the module and controller are being created in a separate file and save this file with the .js extension. Link this file in the src attribute of the <script> tag.
index.html:
Explanation: In the above example we have made a separate .js file to write the code for the controller. In the index.html we have created the AngularJS app and imported the app.js file to use the properties of the controllers.
Output:
Example 3: In this example, we will be performing the basic arithmetic operations on 2 numbers entered by the user.
Explanation: In the above example, we created different functions for different operations inside the controller. When a button is clicked the function specified in the ng-click directive is called and the answer is calculated and displayed.
Output: When the user enters two numbers and clicks on Add button the two numbers are added and the output is shown. Similarly, the output will be shown when the buttons Subtract, Multiply and Divide are clicked.