Controller

AngularJS application mainly relies on controllers to control the flow of data in the application and controller should only contain the business logic needed for a single view. 

Note: If we don’t have custom implementation then we don’t need Module. If we have any kind of custom implementation then we can implement our business logic through a controller. 
 
A controller is a basic structure used in Angular to preserve scope and handle certain actions within a page. Each controller is coupled with an HTML view.
Syntax:
<html ng-controller=”controllerName”>
</html>
 <script> app.controller(‘controllerName’, controllerFunction); </script>
Step to create Angularjs Controller
When creating angularjs controller object, angular framework follow the following step
Step 1. Register the controller to angular Framework.
       var app = angular.module(“myApp”, []);
      app.controller(“myCtrl”, function($scope)
Step 2: Create angular directive
We have to create controller object and bind that particular controller to view in our template through directive called ng-controller.
example :
<body ng-controller=”myCtrl“>
If we don’t define ng-controller name in our view, then Angularjs framework will not create controller instance. When we define ng-controller directive in view, then we need to inform Angularjs Framework to define controller object.  In AngularJS, a Controller is defined by a JavaScript constructor function that takes argument $scope and we can use $scope to access the data binding.
Internally the angularjs called $controller object. this $controller object is parent or or factory function which is going to create all our custom controller objects.  For our example we have myCtrl controller object, we required $scope object to access the binding which is made between.
<div ng-controller=”myCtrl“>
……
</div>
To create controller myCtrl, $injector will take help of $controller as
    $inject:$controller(“myCtrl”, {‘$scope’: $rootScope.$new() });
All the binding is wrapped inside the $rootScope and through $rootScope we create $scope object. We can access all binding through $scope object.
The $scope object is created from the $rootScope by using a method called $new. We don’t need to do anything, everything is done internally as Angular will handle this through dependency injection principle. Whatever object we required for our apps, all the object is created by Angular itself. For that purpose, we take help of $injector.

Note: All our object is created by $injector.  In the example below, we have injected only one object in a controller that is $scope.

<html ng-app="myApp">
<html>
<script src="js/angular.min.js"></script>
<body ng-controller="myCtrl">
<div >
  <p>Enter the First Number : <input type="number" ng-model="firstnum"></p>
  <p>Enter the Second Number : <input type="number" ng-model="secondnum"></p>
  <button ng-click="sum()">+</button> |
  <button ng-click="sub()">-</button> |
  <button ng-click="mul()">*</button> |
  <button ng-click="div()">/</button>

  Full Name : <label ng-bind="result"></label>
</div>

<script>
  var app = angular.module("myApp", []);
  app.controller("myCtrl", function($scope){
    $scope.firstnum = 0;
    $scope.secondnum = 0;
    $scope.result = 0;
    $scope.sum = function(){
      $scope.result = $scope.firstnum + $scope.secondNum;
    }

    $scope.sub = function(){
      $scope.result = $scope.firstnum - $scope.secondNum;
    }

    $scope.mul = function(){
      $scope.result = $scope.firstnum * $scope.secondNum;
    }

    $scope.div = function(){
      $scope.result = $scope.firstnum / $scope.secondNum;
    }
  });
</script>
</body>
</html>