top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

    Get Free Article Updates

Facebook Login
Site Registration
Print Preview

Explain Module And Controller In AngularJS.

+1 vote
33 views

AngularJS module is nothing but a container of all angular components like controller, services, directive, filter, config etc

What is Module

Let me explain why module is required in AngularJS. In .NET console application there is a main method and what main method does is, it’s an entry point of application. It is the same as angular module and is an entry point. Using module we can decide how the AngularJS application should be bootstrapped.

We can create a simple module using the following code.

var myApp = angular.module(‘myModuleApp’,[]);   

In the above code myModuleApp is the module name and if this module is dependent on other modules we can inject in “[]”.

What is Controller?

Controller is a JavaScript constructor function which controls the data. I am not going to cover what are the types of functions in this article but let me give some brief information about constructor function. In constructor function when we call that function that function creates a new object each time.

Let’s make a controller.

myApp.controller(‘myController’, function($scope)  

{  

  

});  

controller

posted Sep 28 by Shivaranjini

  Promote This Article
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button


Related Articles

A controller is a set of JavaScript functions which is bound to a specified scope, the ng-controllerdirective. Angular will instantiate the new controller object, and injects the new scope as a dependency. It contains business logic for the view and avoids using controller to manipulate the DOM. 

controller

Controller Rules

  • We can use controller to set up the initial state of the scope object and add behavior to that object.
  • We do not use controller to manipulate DOM. It should contain only business logic and can use data binding and directives for the DOM manipulation.
  • We do not use controllers to format input but can use angular from controls instead of that.
  • We do not use filter output but can use angular filters instead of that.
  • We do not use controllers to share code or state across controllers but can use angular services instead of that.
  • We do not manage the life-cycle of other components.

Creating a Controller

  • Requires ng-controller directive.
  • Add controller code to a module.
  • Name your controller based on functionality.
  • Controllers are named using camel case (i.e. SimpleController).
  • Setup the initial state of the scope object.

ng-Controller directive

ng-Controller directive is an associated controller class to the view.

How to use ng-Controller

<Any ng-Controller=”expression”>  

</Any>  

<div ng-app="mainApp" ng-controller="SimpleController">  

</div>  

READ MORE

AngularJs : 

AngularJS is a JavaScript framework. It was originally developed by Hevery and Adam Abrons. Now it’s maintained by Google. AngularJS is lightweight and easy to learn. It is perfect in Single Page Application projects. AngularJS is open source, free of cost, and easy to handle. 

 

Currency Filter

 

One of the filters in AngularJS is the Currency Filter. This “currency” filter includes the “$” Dollar Symbol as the default. So we can use the following code as the html template format of Currency Filter. 

  1. {{ currency_expression | currency : symbol : fractionSize}}  

Code

 

Html :

 

The following html code contains the root directory “ng-app” & Controller “ng-controller”. So you can create any name for it. The AngularJS currency filter contains the “$” Dollar symbol as default

<div ng-app="CurrencyApp" ng-controller="CurrencyController">  

   <input type="number" ng-model="amount">  

   <h2>{{ amount | currency }}</h2>  

</div>  

AngularJS

 

When we run the following code you will get the result as “$1000”. So you can change this “$” Dollar symbol based on the html template format of Currency Filter.

var myApp = angular.module('CurrencyApp', []);  

myApp.controller('CurrencyController'function($scope) {  

  $scope.amount = 1000;  

});  

Removing Default Symbol Of Currency Filter 

 

Html

 

In the following code value=”” , So the default Symbol “$” Dollar will be removed. 

<div ng-app="CurrencyApp" ng-controller="CurrencyController">  

      

   <input type="number" ng-model="amount">  

   <h2>{{ amount | currency : value="" }}</h2>  

   <!-- or-->  

   <h2>{{ amount | currency : "" }}</h2>  

   

</div>  

 

List Of Countries and Their Currency Using Angular Js 

 

Html

 

The following code contains the currency symbol for respective countries. 

<h3>Countries & Their Currency Using AngularJS</h3>  

<div ng-app="CurrencyApp" ng-controller="CurrencyController"> <input type="number" ng-model="amount">  

    <h3>    

   Removed default currency($) :  {{ amount | currency : "" }}      

</h3>  

    <h3>    

   Default currency($) :  {{ amount | currency }}      

</h3>  

    <h3>    

   Indian Currency: {{amount | currency:"₹"}}    

</h3>  

    <h3>    

   US Dollar - USD :  {{ amount | currency : "$" }}      

</h3>  

    <h3>    

   United Kingdom Pound - GBP : {{amount | currency:"£"}}    

</h3>  

    <h3>    

   Thailand Baht - THB : {{amount | currency:"฿"}}    

</h3>  

    <h3>    

   China Yuan Renminbi - CNY : {{amount | currency:"¥"}}    

</h3>  

    <h3>    

   Costa Rica Colon - CRC : {{amount | currency:"₡"}}    

</h3>  

    <h3>    

    Ukraine Hryvnia - UAH  : {{amount | currency:"₴"}}    

</h3> </div>    

AngularJS

var myApp = angular.module('CurrencyApp', []);  

myApp.controller('CurrencyController'function($scope) {  

  $scope.amount = 1000;  

});  

 

Fraction Size In AngularJS Currency Filter 

 

We can use the fraction size in the currency filter. In the following code we added 3 as the fraction size so it added 3 zeros in the result. 

 

Html  

<h3>Fraction Size In AngularJS Currency Filter</h3>  

   

<div ng-app="CurrencyApp" ng-controller="CurrencyController">  

  <input type="number" ng-model="amount">  

<h3>  

    Amount :  {{ amount | currency : "$" : 3 }}    

</h3>  

</div>  

READ MORE
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...