Angular 4 Directives – Lesson 3

Hi friends, in the last session we have learnt about how to setup in the local environment. In this session we will learn about Directives. Topics that are going to be covered in this session are

  1. Structural directives (Built-in Directives (ngIF, ngFor, ngSwitch)
  2. Style and Class Directives ( ngClass, ngStyle)
  3. Attribute directives
  4. Host Listener and Host Binding


Overview of Directives:

Directives are the classes that can change the behaviour or appearance of the components by using CSS Classes, CSS Styles and events.

There are three kinds of directives in Angular:

  1. Components—directives with a template.
  2. Structural directives—change the DOM layout by adding and removing DOM elements.
  3. Attribute directives—change the appearance or behavior of an element.

Components are the most common of the three directives. You saw a component for the first time in the QuickStart example.

Structural Directives change the structure of the view. Two examples are NgFor and NgIf in the Template Syntax page.

  • We use (*) before the structural directives, and when we use (*) before ngIf/ ngFor, angular actually recognizes that the current element as a template and it will read the current elements as <ng-template>. Where as in Angular 2.X it reads as <template>.

Attribute directives are used as attributes of elements. The built-in NgStyle directive in the Template Syntax page, for example, can change several element styles at the same time.


*ngIf:  This directive is used for conditional check on the HTML targeting elements. This will work similar to ng-if in Angular 1.X, where the elements will not get rendered into DOM until the condition satisfies. 

Step1: From the quick start example edit the component.ts

File: app.component.ts

Step2: Create a template.html in the app folder

File: template.html

Note: when use * before ngIf, angular treats it as <ng-template>


*ngIf else:  Angular 4  *ngIf has if/else syntax that make sense when you are making ajax calls we can show loading within the page. Where as else block is not existed in Angular 2.

From the above example, add the following html in the template

File: template.html

Angular compiler (JIT or AOT) transforms any <ng-template> elements and directives used with an asterisk (*) into views that are separate from the root component view.

Here when we use (*) before ngIF, angular actually recognizes that the current element as a template, and angular reads it as,


*ngIf then else: *ngIf directive can use two templates, one for If condition and the other for else condition. In the previous example we have seen using template for else condition, now for If condition we need to use then statement.

File: template.html


*ngFor: *ngFor directive is used to iterate through each element of any array, it works similar to For Loop in JavaScript, where in JavaScript we will write for loop within the <script> tag, but in angular we use *ngFor in the HTML as directive for any kind of elements.

From the above example add the following code in the component file

File: app.component.ts

File: template.html

Here we are using for..of, where

  1. for..of iterates through items and returns values
  2. iterates through properties and  returns list of keys
  3. let is a keyword which is a dynamic datatype of ECMA script.
  4. emp is a temporary variable that hold the current employee from the employees array.
  5. employees is the array of items

Here the let can also create a index as a reference of the current item.

Here index is a built-in keyword of angular framework which can be used in *ngFor. Where angular renders the above code as follows.


*ngFor with select tag:


ngSwitch: ngSwitch works similarly the switch statement that we use in C# / java etc., Here for defining case we use *ngSwitchCase and for default case we use *ngSwitchDefault

From the above example using the same list of employees, we have created a example

Setup for local environment in Visual Studio – Lesson 2

Setting up Angular2/Angular4 is a quite complex, it needs a lot of work when compared with the installation for Angular1.X applications.

To setup with the quick start files follow these steps:

Step1: Install Node JS and npm

Step2: Install visual studio 2015 update3

Step3: Configuring External Web Tools.

Step4: Install Typescript

Step5: Download quick start files

Step6: Create ASP.NET Project

Step7: Copy the quick start files into your ASP.NET Project

Step8: Restore Packages

Step9: Build and Run

Install Node and NPM
  • Angular 2/4 is developed with compatible latest versions of Node, where you must have Node JS version 4.6.X or higher and npm 3.X.X or higher.
  • Older versions produce errors
  • Before installing make sure whether you have already installed Node JS before. For checking the Node JS and npm versions follow these commands in command prompt.

For Node JS: node –v

For npm: npm –v

  • If the PC don’t have Node JS installed it will show you like “node” is not recognized as an internal or external command, “npm” is not recognized as an internal or external command. Or if your PC has older version than the required please update to the latest versions.
  • For installing latest version of Node JS and npm go to
  • Your first step will be completed if you have installed the required versions.


Install Visual Studio 2015 Update 3

Note: For studio 2017 versions can ignore this step

Minimum requirement for developing Angular2/ Angular4 applications is Visual Studio 2015 Update3 because the earlier versions do not follow the best practice for developing applications with Typescript.

  • To check the version of Visual Studio 2015

Open Visual Studio 2015    go to Help    click on About Microsoft Visual Studio

  • If you don’t have install Visual studio 2015 update 3


Configure External WebTools
  • We need to configure Visual Studio to use the global external web tools before the tools that ship with visual studio.
  • Go to Visual Studio    Click on Tools    Options    Expand Projects and Solutions    click on External Web Tools    now in the right hand side move $(PATH) entry above the $(DevEnvDir) This tells the visual studio to use the external tools like (npm) found in the global path before using its own version of the external tools.
  • Now click OK and restart the Visual Studio to reflect these changes. Visual Studio now looks first for external tools in the current workspace and if it doesn’t find them, it looks in the global path. If Visual Studio doesn’t find them in either location, it will use its own versions of the tools.


Install Typescript
  • To develop Angular applications we need Typescript 2.2.0 or its higher versions, as Visual studio Update3 doesn’t have such specifications we need to install it externally.
  • Before installation we can check our Typescript version in the Visual Studio

Open Visual Studio 2015    go to Help    click on About Microsoft Visual Studio


Download Quick start files

You can download the quick start files from Git hub and then extract the files.


Create ASP.NET Project

Create ASP.NET 4.X or above project in the visual studio

Go to Visual Studio       File      New     Project    select Visual C# under Templates    click on ASP.NET Web Application     select Desired Templates    give the project name and click on OK.


Copy Quick start files into ASP project

Copy the quick start files into your project, but all the files are not necessary to be included into your project. So copy only selected files from the extracted folder.

Copy the following files into your .csproj

  • Src
  • Bs-config.json
  • json
  • json


Restore  Packages

Restore the packages required for your application

  • For restoring packages right click on json     select Restore Packages. This uses npm to install all the packages defined in package.json.
  • In between you can open the Output window to watch the npm commands that are executed. Simply we can ignore the warnings.
  • After you see the message Installing Packages Complete make sure that your project doesn’t includes node_modules


Build and Run

 File: /src/app/component.ts

In the above example that we have copied from the quick start files,

  1. In the first statement we are importing Component Package from angular/core package directory. After restoring package.json in /node_modules you can see all the packages that are downloaded using npm commands.
  2. In the second statement according to angular,
    1. @ is called –> decorator
    2. @Component  is called –> Component decorator
    3. selector –> xml tag / custom html tag
    4. template –> view for the component

Complete syntax of component, (Component decorator + class followed by the component decorator)

3. In the third statement we are exporting the component, to make it visible to other modules in our angular application.

  • After Installing packages build the Application.
  • To run the application open Command Prompt     go to your Project Directory       execute command npm start.
  • When this command is executed,
  • firstly it will launch typescript compiler and compiles the application with the following command tsc –p src/
  • Now it will start the lite-server and launches the browser where you can see the

Output: Hello Angular

  • But if you run the html by pressing F5 in visual studio it will not show the same output.

To Run in VS with F5 follow these steps

    1. In html, change base href from <base href=”/”>to <base href=”/src/”>.
    2. Also in html, change the scripts from node_modules to /node_modules with a slash
    3. In src/systemjs.config.js, near the top of the file, change the npm pathto /node_modules/ with a slash.
    4. After these changes npm start command no longer works, you must choose to configureeither for F5 with IIS or for npm start with the lite-server.
    5. Now run the application using F5

Tips to become Experienced Programmer

 “Experience is just a number”, in fact there is nothing difference between a Fresher and Experience. Every experience person also feels the same. When you are Fresher it doesn’t mean that you are less Intelligent / Skilled than Experience, you just need some time to stabilize your skills, intelligence, logical thinking and patience. Time will teach you everything. Most important difference is “Patience”, experience people will always show patience in any work. If you are a Fresher just keep some key points into your mind while coding, “That’s it”. Those key points will make you very closer to an Experience programmer/developer.

Key points to change you to an Experience programmer/developer:

  1. Learn how to Google
  2. Grip on OOPS
  3. Code Reuse
  4. Code comments
  5. Follow Standards


Learn how to Google:

  • Most of the Experience developers in IT sector are surviving because of If you know how to Google for a solution no doubt you can survive anywhere in IT sector. But need to remember is “Compromise nowhere until find the solution”.
  • Experience Developer:
    • Analyze the problem first
    • Google with the exact keywords
    • Picks up the popular forum sites like (stack overflow, Code project, Code Guru etc…)
    • start applying the up voted answers first
    • If not working, go for the all the other solutions and keep trying
    • if not working till, now he/she will start learning that new concept from technical content sites.

Note: As you have seen from the above process, “No where the person give up“. Just keep on trying till you find the solution, if not take your colleagues help.

Google API’s working formula

Minimum and simple words     Maximum results

For Example:

  • Who is the author of C (many words) gives few results
  • Author of C (few words)  good results

  • Prefer stack overflow first (Note: if it’s a known concept for you)
    • Some of the popular discussion forum sites (stack overflow, Code project, Code Guru etc…)
    • In the stack overflow the very first step search for the “Most up voted answer”.
    • “Read all the solutions”. Many solutions will work even if it’s not an up voted and vice versa.
  • Prefer site which has easy slang according to your terminology. If you pick the toughest slang of technical terminology you will never understand that topic in depth.


Grip on OOPS:


Strong in OOPS       you will argue with someone

Weak in OOPS         you will listen to someone

Confused wright!!

Yes when you are strong at something (For example: You know very well HTML). If your colleague doesn’t follow standards then what will be your reaction, sure you will argue with him and teach him the right way.

  1. Concentrate more and more on Loops (for, while, do while).
    1. If you are not confident on loops write every iteration on paper for some days.
    2. Iterate loops in your mind once you are confident.
  2. Use all oops concepts in every module if it’s not even required. So that you will not forget it.
  3. Teach someone who doesn’t know OOPS, teaching is the best-way to remember the basics all the time.
  4. Concentrate on unknown topics and learn at least one topic a day.


Code reuse

Most of the Experience Developers follow Code Reusing techniques all the time. Few techniques are using

  • Inheritance
  • Libraries (DLL’s)
  • Web Services & Web API’s


  1. Don’t ever write every piece of code in the same file
  2. Create libraries (or) Packages and Reuse them for Business Logics.
  3. Always prefer multi-layered architecture in all the Tiers of project. So that presentation of your code will be very clean.
  4. Create API’s for the Repositories (Repositories are noting but your Data Access Layer).
  5. Always try to maintain Loose coupling e., use Dependency Injection. This will be very helpful to test the application.
  6. Use Inheritance as much as you can.


Code Comments:

Few places where you should write comments:

  • File level / Class level comments
  • Method level comments
  • Code level comments


  1. Your code behind file (or) JavaScript file should look more green (or) red in color (color of comment depends on the language).
  2. Write a brief description on the top about complete file (Class file / script file) with Block Comment.
  3. Write XML comments on every method about the inputs, output, exception handling, date modified, and modified by.
  4. Write comments for every two or three lines, and for every code block in a function/ method.

Block Comment

XML comments

Follow Code Standards:

  1. Always use Pascal case (or) Camel case
  2. Don’t use names begin with numeric characters.
  3. Do not use Hungarian Notation!

Example: strName or nCount

  1. Don’t include the parent class name within a property name.

Example: Use Employee.Name NOT Employee.EmployeeName

  1. Give prefixes to Boolean variables using “IS”, “Has”, “Can”.
  2. Every developer has their own coding style.
  3. Don’t give more than one empty line in any piece of code
  4. Don’t write multiple classes in same file.

Connected and Disconnected Architectures in DotNet

  • NET (Active x Data Object) is mediator between front end and back end that interacts with client side application and server side application which supports two types of Data Accessing models, one is Connection oriented and Disconnected oriented.
  • NET leverages the power of XML for accessing data using disconnected architecture. It was completely designed with XML classes in .NET Framework.


Note: Before going into this article you should have a basic understanding on SQL queries on select, insert, update and delete commands.

Connection String

Connection string plays very crucial role in connecting your front end application and back end application. It’s a normal string which contains the information to establish the connection between backend Database and application and secured information such as user Id and password.

  • Usually Data Providers uses this connection string which contains parameters that are needed for establishing the connection.
  • Let’s have a look at Data Providers that are supported by .Net Framework.

List of Data providers which is supported by .Net Framework.

SQL server Provider connection string:

OLEDB Provider connection string:

ODBC Provider connection string:


Connection Oriented Architecture


The architecture which needs an open connection to access the data from database is known as Connection Oriented Architecture.

Following are the list of classes on which Connection Oriented Arch is built on:

  • ConnectionUsed to establish the connection between front end and back end.
  • CommandEnables access to database commands to return data, modify data, run stored procedures, and send or retrieve parameter information
  • Data Reader Used to read the data from Source. Provides high performance stream of data from data source.
  • Data Adapter
  1. Mediator between front end and back end which doesn’t have feature of containing data with in this , so it uses Dataset which is a result set.
  2. Also provides bridge between DataSet and data source.
  3. Use Command Objects to execute commands.

As Connected architecture needs connection for every transaction and creates much traffic to database since it do several trips. It shows very high impact on performance when the transaction has many commands i.e. for larger transactions. If it’s doing smaller transactions it is normally much faster.

Note:  In this Connected oriented arch the application stays connected even when it’s not doing any operations on database. To overcome this ADO.NET introduced a new component called DataSet which is a central component in Disconnected Architecture.

DataSet ds = new  DataSet( );


Disconnection Oriented Architecture


The architecture in which data can be retrieved from database even when the connection gets disconnected or closed is known as Disconnected Oriented Architecture.

Following are the list of classes on which disconnected arch is built on:

  • Connection
  • Command Builder Can generate insert, update and delete commands on single data base table for a data adapter.
  • Data Adapter
  • Data Set Contains the set of Data Tables (Data Table is set of Data Rows and Data Columns)

  • Data View It’s a view of table available in Can be used to perform insert, update and delete commands as in case of Data Set. Can be used to find, sort and filter the records.

  • Disconnected architecture is a method of retrieving the result set from database and giving the user ability to perform all CRUD operations like insert, update and delete. No traffic issues will get occurred because even though connection was gone data will be get already stored into dataset object.
  • By keeping connections open for only a minimum period of time, ADO .NET conserves system resources and provides maximum security for databases and also has less impact on system performance.
  • Any changes to data in DataSet doesn’t show effect on database directly, to save the changes need to use Update() method of Data Adapter.
  • Can be used with multiple and differing data sources, used with XML data, or used to manage data local to the application.


Da.Update(ds,”Table Name”);


Differences between Connection and Disconnection Oriented Architecture




Differences between Java and DotNet

  • Java and .Net are two incomparable guided, but by fact that .Net is Framework of simply platform and supports for almost 63 languages and java is just a programming language.
  • These two technologies have different syntax but gives the same result.
  • Let’s look at some basic imports and usings used in java and .Net.

  • Now let’s look at some application based differences.

  • Now let’s look at some basic differences.