Sunday, June 12, 2016

Angular2 for Angular1.x developers

If you are an angular 1.x developer and trying to migrate in to Angular2 here are some points which you need to keep in mind.

1. Where are singleton factories and services ,

     Angular1.x    We had lazy singleton  instances of services and factories for entire scope of the application. We had one injector for entire app who is responsible creating an instance of a service or a factory when it is requested by some component.
     Angular2     Here we have a hierarchical set of interjects , which makes a tree similar to component tree in the application. That makes previous concept of singletons little bit complex.  Because we can have different instances of same class in different injectors that means in different levels in the components tree. Here how its done,

Create Application level singleton (This is discouraged ) ,

 1. Bootstrap the application with the provide specified
 2.And then don't specify  the service name in the metadata for the component ,

  1. @Component({
  2. selector: 'my-component',
  3. template: `
  4. Some template
  5. `,
  6. providers:[YourServiceForEntireApp]// Don't do this
  7. })
The component level injector will look for the instance of  YourServiceForEntireApp in the current component level injector since it is not there it will look upward and get the application level instance.This is discouraged because in most of the time a service will responsible for a part of the application.

 Create singleton for part of the application

1. Specify service in provider section in the parent component which cover the area of the application which need to keep a singleton instance
  1. @Component({
  2. selector: 'my-parent-component',
  3. template: `
  4. <h2>Parent</h2>
  5. <router-outlet></router-outlet>
  6. `,
  7. providers:[YourServiceSingletonUnderthisComponent],
  8. })
  9. export class HeroesComponent { }

 2. And make sure you don't specify YourServiceSingletonUnderthisComponent in  providers of child components under this.


  1. The blog is very informative and mind blowing. I think everyone should know such information like you have described on this post. Thank you for sharing the blog.
    Car Wash Services in Mumbai

  2. Very nice post here thanks for it I always like and search such topics and everything connected to them. Keep update more information..

    Back to original

  3. This blog is having the general information. Got a creative work and this is very different one.We have to develop our creativity mind.This blog helps for this. Thank you for this blog. This is very interesting and useful.

    digital marketing company in india

  4. I do agree with all the ideas you have presented in your post. They’re really convincing and will certainly work. Still, the posts are very short for newbies. Could you please extend them a little from next time? Thanks for the post..
    Skilled manpower services in Chennai
    Housekeeping services in Chennai
    House cleaning service in Chennai

  5. A nice article here, i think that people who have grown up with the idea of using computers are showing more responsibility towards writing posts that are thoughtful, do not have grammar mistakes and pertinent to the post..
    Salesforce Training in Chennai
    PHP Training in Chennai
    Informatica Training in Chennai