In this article, we will see the Controller in AngularJS along with knowing how Controller works, the concept of the Controller method & how the Controller can be implemented in an external. We will understand all these aspects with the help of their implementation & accordingly will its illustrations.
<element ng-controller="expression"> Contents... </element>
Example 1: This example implements the ng-controller Directive to display the concatenation of the First and Second input of a user.
Controller method: This first example demonstrated a controller object with two properties: Fname and Lname. In AngularJS, the function($scope) is defined when controller is defining and it’s return the concatenation of the $scope.firstname and $scope.lastname. Here a controller can also have methods (variables as functions):
Example 2: This example illustrates the basic implementation of the Controller method in AngularJS.
Controllers in External files: In larger applications, it is common to store controllers in an external file, you just need to copy that file name and paste it into a <script> tag. Here the external file name is “script.js”
Example 3: This example illustrates defining the separate external file to define the controller in AngularJS.