Sunday, May 1, 2016

Components in angular 1.5.x

In my previous article I described my view on a component based architecture. In this post I want to focus on how I applied this in a real life application with angular. Since the 1.5 version was released, components have become first class citizens just like in Angular 2, Ember 2, React,… On the other hand components aren’t so revolutionary. We have already known them for a long time as directives, but we never used them like this. Generally, we would only use directives for manipulating the DOM directly or in some cases to build reusable parts.
The components in angular 1.5 are a special kind of directive with a bit more restrictions and a simpler configuration. The main differences between directives and components are:
  • Components are restricted to elements. You can’t write a component as an attribute.
  • Components always have an isolated scope. This enforces a clearer dataflow. No longer will we have code that will change data on shared scopes.
  • No more link functions, but only a well-defined API on the controller.
  • A component is defined by an object and no longer by a function call.

Well-defined Lifecycle

Components have a well-defined lifecycle:
  • $onInit: This callback is called on each controller after all controllers for the element are constructed and their bindings initialized. In this callback you are also sure that all the controllers you require on are initialized and can be used. (since angular 1.5.0)
  • $onChanges: This callback is called each time the one-way bindings are updated.  The callback provides an object containing the changed bindings with their current- and previous value. Initially this callback is called before the $onInit with the original values of the bindings at initialization time. This is why this is the ideal place for cloning your objects passed through the bindings to ensure modifications will only affect the inner state.
    Please be aware that the changes callback on the one-way bindings for objects will only be triggered if the object reference changes. If a property inside the object changes, the changes callback won’t be called. This avoids adding a watch to monitor the changes made on the parent scope (works correctly since angular 1.5.5)
  • $postLink: This is called once all child elements are linked. This is similar to the (post) link function inside directives. Setting up DOM handlers or direct DOM manipulation can be done here. (since angular 1.5.3)
  • $onDestroy: This is the equivalent of the $destroy event emitted by the scope of a directive/controller. The ideal place to clean up external references and avoid memory leaks. (since angular 1.5.3)

Well-defined structure

Components also have a clean structure. They exist out of 3 parts:
  • A view, this can be a template or an external file.
  • A controller which describes the behaviour of the component.
  • Bindings, the in- and outputs of the component. The inputs will receive the data from a parent component and by using callbacks, will inform the parent component of any changes made to the component.


Because components should only be allowed to modify their internal state and should never modify any data directly outside its component and scope. We should no longer make use of the commonly used two-way binding (bindings: { twoWay: “=” } ). Instead since angular 1.5 we now have a one-way binding for expressions (bindings: { oneWay: “<” } ).
The main difference with the two-way binding is the fact that the bound properties won’t be watched inside the component. So if you assign a new value to the property, it won’t affect the object on the parent. But be careful, this doesn’t apply on the fields of the property, that is why you always should clone the objects passed through the bindings if you want to change them. A good way to do this is working with named bindings, this way you can reuse the name of the bound property inside the component without affecting the object in the parent scope.
   1: module.component("component",{
   2:     template: "<div>{{$ctrl.item}}</div>"
   3:     bindings: {
   4:         inputItem: "<item"
   5:     }
   6:     controller: function(){
   7:         var $ctrl = this;
   8:         this.$onChanges = function(changeObj){
   9:             if(changeObj.inputItem){
  10:                 this.item = 
  11:                     angular.clone(changeObj.inputItem.currentValue);
  12:             }
  13:         }
  14:     }
  15: });
Another way to pass data is by using the “@” binding. This can be used if the value you are passing is a string value. The last way is using a “&” binding or a callback to retrieve data through a function call on the parent. This can be useful if you want to provide an auto complete component with search results data.
For exposing the changes inside the component we can only make use of the “&” binding. Calling a callback in the parent scope is the only way that we can and should pass data to the parent scope/component.
Let’s rephrase a little:
  • “=” binding (two-way) should no longer be used to avoid unwanted changes on the parent’s scope.
  • “<” binding (one-way) should be used to retrieve data from the parent scope passed as an expression.
  • “@” binding (string) should be used to retrieve string values from the parent scope.
  • “&” binding (callback) can be used to either retrieve data from the parent scope or be used as the only way to pass data to the parent scope.


The components way of working in angular 1.5 closes the gap for migrating your code to an angular 2 application a bit more. By building your apps in the angular 2 style you are already thinking on the same level. This will ease the migration path by shifting away from the controller way of working.
Directives will still have an existence beside the components. You will still have some cases where you will want to use attributes instead of elements or have the need of a shared scope for the UI state. But in most cases components will do the trick.


  1. Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting. So know it's helpful..
    Veritas Volume Manager Training in Chennai

    1. I have read your blog its very attractive and impressive. I like it your blog.

      SEO Services in India SEO Company in India SEO Company in India

      Guaranteed SEO services Guaranteed SEO

  2. I am not sure the place you are getting your information, however good topic. I needs to spend some time studying more or understanding more. Thank you for wonderful information I was in search of this info for my mission.

    Digital marketing company in Chennai

  3. There are lots of information about latest technology and how to get trained in them, like this have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies. By the way you are running a great blog. Thanks for sharing this.
    Android training in chennai

  4. Great and useful article. Creating content regularly is very tough. Your points are motivated me to move on.

    SEO Company in Chennai

  5. Great article. Happy to visit your blog. Thanks for sharing.

    web design institute in chennai

  6. i'm really happy to visit your blog.really interesting.thank you for sharing this blog.
    BigData Analytics Training

  7. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.

    Core JAVA Training in Chennai | JAVA Training in Chennai

  8. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Pearson Vue Exam Center in Chennai | Certification Exam Centers in Chennai

  9. The blog gave me idea about Components in angular 1.5.x Thanks for sharing it
    Hadoop Training in Chennai

  10. Thanks for sharing this informative content which provided me the required information about the latest technology.
    Software Testing Training Institute in Chennai | Software Testing Training in Chennai | Online Training Institute in Chennai

  11. That looks really helpful. I would like to get more ideas on the topic. It would be helpful if you'll share some educated sources here.

  12. hi welcome to this blog. really you have post an informative blog. it will be really helpful to many peoples. thank you for sharing this blog.
    java training in chennai

  13. Thank you for sharing those thought. I think that I could use your decision in my own work. Thank you!

  14. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.

    iOS App Development Company

  15. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharng this information,this is useful to me...
    Android training in chennai
    Ios training in chennai

  16. Nice article, I loved it. Thanks for sharing and keep sharing. Get a ERP software for you small business.For more details contact us.
    erp in chennai
    cloud erp in chennai
    leather erp in chennai
    trading erp in chennai
    franchise erp in chennai

  17. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.
    Android App Development Company

  18. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

  19. I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
    iOS App Development Company
    iOS App Development Company

  20. You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...
    Fitness SMS
    Fitness Text
    Salon SMS
    Salon Text
    Investor Relation SMS
    Investor Relation Text

  21. it is really amazing...thanks for sharing....provide more useful information...
    Mobile app development company

  22. Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.

    Android Training in chennai | IOS Training in chennai

  23. Hi, i really enjoyed to read your article.. i got clear idea through your views and ideas.. thanks for sharing your post..

    Java Training in chennai

    Software Testing Training in chennai

  24. great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
    web design Company
    web development Company
    web design Company in chennai
    web development Company in chennai
    web design Company in India
    web development Company in India

  25. This article is very much helpful and i hope this will be an useful information for the needed one. Keep on updating these kinds of informative things...
    Texting API
    Text message marketing
    Digital Mobile Marketing
    Sms API
    Sms marketing

  26. Pretty and nice article, thanks for sharing your views and ideas.. it helps me lot to learn my technical skills..
    Embedded Project Center in Chennai | Embedded Project Center in Velachery

  27. Excellent post. Keep updating.I have got some useful information form this article.
    Best Real Time Project Center in Chennai | Real Time Project Center in Velachery

  28. Interesting post! This is really helpful for me. I like it! Thanks for sharing!
    regionale jobbörse

  29. Thank you so much for sharing this great blog.Very inspiring and helpful too.

    AngularJS Training in Chennai
    Best AngularJS Training in Chennai

  30. Good post and I like it very much. By the way, anybody try this increase app downloads? I do not how to use.

  31. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man,Keep it up.

    PSD to Wordpress
    wordpress website development

  32. Great article. If you want to learn more on Angular's latest version, Angular 2/4/5 - do visit this web link: Angular Training in Chennai

  33. Like that components! Your blog is like professional! Do you want to find best premium wordpress templates for any kind of business? go here

  34. Thanks much for creating and sharing this wonderful article, like to read this regularly to get more updates...
    Best Online Software Training Institute | Angular 5 Training


  35. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.
    Angularjs course in chennai