Angular js modules and controllers

What is a module in AngularJS

A module is a container for different parts of your application i.e controllers, services, directives, filters, etc. 
In this Tutorial we will also discuss controllers. We will discuss services, filters and directives in a later Tutorial. 

Why is a module required

You can think of a module as a Main() method in other types of applications. 
For example, a Dot Net console application has a Main() method which is the entry point into the application and it wires together the different parts of the application.

Modules are the angular application's equivalent of the Main() method. 
Modules declaratively specify how the angular application should be bootstrapped. 

There are several benefits of the modular approach. 
It may be difficult to comprehend all those benefits right now, 
so we will defer the discussion of the benefits to a later Tutorial.

How to create a module

Creating a module in angular is staright forward. 
Use the angular object's module() method to create a module. 
The angular object is provided by angular script. The following example, creates a module. 

var myApp = angular.module("myModule", [])

The first parameter specifies the name of the module. 
The second parameter specifies the dependencies for this module

A module can depend on other modules. We will discuss an example of module dependencies in a later Tutorial. Right now, the module that we are creating is not dependent on any other external modules, 
so I am passing an empty array as the value for the second parameter.

What is a controller in angular

In angular a controller is a JavaScript function. 
The job of the controller is to build a model for the view to display. The model is the data. In a real world application, the controller may call into a service to retrieve data from the database.

How to create a controller in angular

Simple, create a JavaScript function and assign it to a variable

var myController = function ($scope) {
    $scope.message = "AngularJS Tutorial";
}

What is $scope

$scope is a parameter that is passed to the controller function by angular framework. 
We attach the model to the $scope object, which will then be available in the view. With in the view, we can retrieve the data from the scope object and display.

How to register the controller with the module
Use module object's controller function to register the controller with the module

myApp.controller("myController", myController);

Here is the complete code 

//Create the module
var myApp = angular.module("myModule", []);

//Create the controller
var myController = function ($scope) {
    $scope.message = "AngularJS Tutorial";
}

// Register the controller with the module
myApp.controller("myController", myController);

The above code can also be written as shown below

//Create the module
var myApp = angular.module("myModule", []);

// Creating the controller and registering with the module all done in one line.
myApp.controller("myController", function ($scope) {
    $scope.message = "AngularJS Tutorial";
});

How to use the module that we created to bootstrap the angular application
Associate the module name with ng-app directive
ng-app="myModule"

Similarly associate the controller using ng-controller directive
ng-controller="myController"

Example 

<!DOCTYPE html>
<html>
<head>
    <title> Attaching objects to the scope</title>
 <meta charset="utf-8" />
    <script src="../Scripts/angular.js"></script>
    <script>

        var MyApp = angular.module("myModule", []);

        var MyController = function ($scope) {
            $scope.message = "My first example of $scope";
        };

        MyApp.controller("MyController", MyController);


    </script>
</head>
<body>
    <div ng-app="myModule">
        <h1>Attaching Object to the scope and use in Html access within controller </h1>
        <div ng-controller="MyController">
            {{message}}
            <div>
                {{message}}
            </div>
        </div>
        
       
    </div>
</body>
</html>


output


Get started with angular (ng-App) directive

Get started with angular (ng-App) directive

We will discuss directives, filters, Modules, Routes etc with examples in our upcoming Tutorial  series.

To build angular applications you only need one script file and that is angular.js.

To get the script file visit https://angularjs.org. From here 
1. You can download the angular script file 

To get started with angular
1. Add a reference to the angular script
2. Include ng-app attribute 

What is ng-app 

In angular, ng-app is called a directive. There are many directives in angular. 
You can find the complete list of directives on https://angularjs.org. 

The ng prefix in the directive stands for angular. 
The ng-app directive is a starting point of AngularJS Application. 
Angular framework will first check for ng-app directive in an HTML page after the entire page is loaded. 
If ng-app directive is found, angular bootstraps itself and starts to manage the section of the page that has the ng-app directive. 

So the obvious next question is, where to place the ng-app directive on the page

It should be placed at the root of the HTML document, 
that is at the <html> tag level or at the <body> tag level, 
so that angular can control the entire page. 

However, there is nothing stopping you from placing it on any other HTML element with in the page. When you do this only that element and it's children are managed by angular. 

Double curly braces are called binding expressions in angular.

Example : In the example below, the ng-app directive is placed at the <html> tag level. 
So the binding expressions in both the div elements are evaluated and displayed as expected. 

<!DOCTYPE html>
<html>
<head>
    <title> Ng-App  Example </title>
    <script src="../Scripts/angular.js"></script>
 <meta charset="utf-8" />

</head>
<body>
    <div>
        <span>Angualr Js is work on ng-App section only </span>
    </div>
    <div ng-app>
        10+20 ={{10+20}}
    </div>
    <div>
        10+20={{10+20}}
    </div>
</body>
</html>

output

Angualr Js is work on ng-App section only

10+20 =30
10+20={{10+20}}

c# most important interview questions

c# most important interview questions

1>  What is Interface 

->Interface is similar to class but interface member cannot have a defination,only have declaration
->In Interface we can not define Explicit Access Modifier by default interface method is public
->we can not define fields in interface like int id, string name 
->We can Inherit multiple interface at the time by using , (comma) seprator
->If we are inherting the interface We Must implement the methods of interface in class
->We Can not create instance of Interface , But an interface reference variable can point to a derived class object.
->If Class Or Structure Inherits from an interface  ,It must provide implemenation for all interface member othrwise we get a comile error.

Example 
 public class Class1
    {
        interface I1
        {
            void Method1();
            void Method2();
        }
        interface I2
        {
            void Method3();
        }

        public class Customer : I1, I2
        {
            public void Method1()
            {
                Console.WriteLine("Print Method 1");
            }

            public void Method2()
            {
                Console.WriteLine("Print Method 2");
            }

            public void Method3()
            {
                Console.WriteLine("Print Method 2");
            }
        }

        public static void Main()
        {
            Customer c1 = new Customer();
            c1.Method1();
            c1.Method2();
            c1.Method3();
            Console.ReadLine();
        }
    }

2> What Is Explicit Interface

If you explicitly implement an interface below is how the code looks like. The difference is that the “Add” and “Update” methods are now private.

public class Person : IDal
{
        public string FirstName { get; set; }
        public string LastName { get; set; }


void IDal.Add()
        {
            Console.WriteLine("Add");
        }

        void IDal.Update()
        {
            Console.WriteLine("Update");
        }
}

In simple words the difference between “Implicit” interface and “Explicit” interface is that in implicit the interface methods are publicly implemented while in explicit the methods are privatelyimplemented.

3>What is structure

->Structure is declare same like class for decalring structure we are using struct keywords.
->In C#, a structure is a value type data type. 
  It helps you to make a single variable hold related data of various data types.
->value type hold there value in memory where they are declared,
  but reference type hold a reference to an object in memory.
-> structure can not have destuctor but class can have destructor.
                                    


-> structure can not have explicit parameterless constructor but class can 
->There is no inheritance for structs as there is for classes. 
  A struct cannot inherit from another struct or class, 
  and it cannot be the base of a class. 
  Structs, however, inherit from the base class Object. 
  A struct can implement interfaces, and it does that exactly as classes do.

4>What is enumeration 

An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword.
C# enumerations are value data type. In other words, enumeration contains its own values and cannot inherit or cannot pass inheritance.

Declaring enum Variable
The general syntax for declaring an enumeration is:

enum <enum_name> 
{
   enumeration list 
};
Where,

The enum_name specifies the enumeration type name.
The enumeration list is a comma-separated list of identifiers.

Each of the symbols in the enumeration list stands for an integer value, one greater than the symbol that precedes it. By default, the value of the first enumeration symbol is 0. 

For example:

enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
Example
The following example demonstrates use of enum variable:

using System;
namespace EnumApplication
{
   class EnumProgram
   {
      enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };

      static void Main(string[] args)
      {
         int WeekdayStart = (int)Days.Mon;
         int WeekdayEnd = (int)Days.Fri;
         Console.WriteLine("Monday: {0}", WeekdayStart);
         Console.WriteLine("Friday: {0}", WeekdayEnd);
         Console.ReadKey();
      }
   }
}