Angular 2 communication using Services

In this article we will discuss about communication using Services in Angular 2. This is very useful when you have more than one components which need to access and manipulate single source of data. This method is usually used in the following scenario:   Service making API hit to server and share data across all the component inside the web app. Service that pull and push data across multiple component because of it’s singleton property (Single source of truth).   Above are just the very common use cases of this methods. There are other lot of situations where you will be …

Continue Reading

Error: Multiple instances of services

When you are working in Angular 2, you might got stuck in a situation where Angular 2 is creating multiple instances of your service. In Angular 2, Service are singleton object which can be used to fetch and push data from multiple components. First of all, try to understand that you only need to add your service once either in @NgModule or AppComponent(Top level component).

If you define your service in both of NgModule & AppComponent provider array than in that case 2 instances of that service will get created. Also Read: execute function inside template – Angular …

Continue Reading

execute function inside template – Angular 2

In Angular 2, You might wanna execute function inside template, that’s what i am gonna help you guy’s quickly. In Angular 2, Simple template interpolation can be done as follow:

Now what if you simply wanna print “NA” if no value corresponding to above interpolation exist. In that case you can either write a function which take above value and return required result as follow:

Now, you can change above template simply as follow:

In above template, i have applied evalDataFields() inside interpolation.  That’s it for this short Q&A… Happy coding…:)    

Continue Reading

Deploy Angular 2 project in Subdirectory

Recently i was working on Angular 2 project using Angular-webpack starter template. When i was done developing whole project on my localhost. I build my project using “npm run build”. I tried loading the content of dist directory to one of my subdirectory on my hosting domain. As soon the upload is done, I got index.html at right place but every each link path is wrong like JS, CSS, Images, font-file etc. I Googled little bit but couldn’t able to find right solution at one place. So that’s why i thought of putting solving this riddle for anyone who will stuck …

Continue Reading

Search pipe in Angular 2

In Angular 1, we have filter pipe for searching Array element. Most application make heavy use of this filter pipe and had speculation of making app heavy from Angular side. Below is the very simple example of Searching a list in Angular 1.

In above example, list will be searched based on input string in input box. filter pipe do everything for us. Now in Angular 2, there are certain pipes available but there is no filter pipe. In angular docs it is mentioned that filter and sort pipes are removed because of Performance issue. They suggest developer to …

Continue Reading

Angular 2 attribute binding

Like class binding, style binding etc, there is also a concept of Angular 2 attribute binding. By using attribute binding you can add data-attribute to element with value as either string or propertyName. In order to understand attribute binding, let’s consider an very simple example.

Above Template will be evaluated as: <div count=2>content</div> This is direct expression interpolation within attribute binding. Other way is too pass propertyName which will finally leave the value of property as value of attribute name as shown below:

Above template will be evaluated as: <div count=10>content</div>

Continue Reading

ngIf, ngFor and ngSwitch in Angular 2

While writing templates in Angular 2, these three directive are used most extensively. These directives are same as Angular 1 but syntax of writing is surely changed. Let’s discuss ngIf, ngFor and ngSwitch in Angular 2 one by one. Above directives are also called as Structural Directives. ngIF: ngIf directive is used to add a conditional check whether that element should be added in DOM. Let’s take a very simple example:

Note: Don’t forget the asterisk (*) in front of ngIf. (*)asterisk symbol before ngIf actually wrap the element inside <template></template>. This * symbol is just there to skip some …

Continue Reading

ng-transclude in Angular 2 Component

I was developing an application lately which requires developing lot of Angular 2  components. I develop components which can be either reused in different part of your application or either separate your code from rest of messy code. while developing components, you  might get into a situation where you want to passing your HTML template in component or ng-transclude as in Angular 1. In Angular 1, we used to do this by adding transclude: true in a directive. Let’s understand how we can achieve ng-transclude in Angular 2. let’s firstly create a simple ItraModalComponent component which allow adding DOM content inside it. Let’s …

Continue Reading

Angular 2 Events & Events Cheet sheet

Angular 2 events are put in template between the parentheses and trigger methods in the component classes as follow:

Angular events are just like any normal event, they are bubble up & propagate down. Angular also provide custom event for component as output  & EventEmitter with the major difference that Angular custom event don’t bubble up, so you can’t bind child event to it’s parent. You can pass the $event for any event in Angular component as an argument as follow, which can be used in a situation where you wanted to stop event propagation.

Beside “click” event there are whole …

Continue Reading

Toggle class on click in Angular 2

This article will explain about how to toggle class on element click event. This is very easy stuff to do in angular. Below is the basic component which has one button and one paragraph tag. So when i click on button, it will toggle it’s class as well as paragraph tag.

So when you click on button “Click Me”, both paragraph tag & button style will get change.

Continue Reading