Angular Components for Beginners

Introduction

 
This article discusses how to create a reusable component in Angular js. This tutorial is for beginners/freshers or students. I have created the functionality of component and used in our program. It is a good idea to create a component and reduce the code of a program in angular js. I hope this will be helpful for you for creating a component in Angular js.
 

Angular Js Code Point

  • ng-app - This directive is used to bootstrap the application. Normally, it is put in a top-level element like HTML or body tag.
  • ng-controller - It is the main component of AngularJS which contains the state and logic both. It acts as a bridge between services and views.
  • $scope - It provides and the link between the data and views. It holds all the required data for the views and used within the HTML template.
  • {{expression}} - It is called expressions and JavaScript-like code can be written inside. It should be used for mainly small operations in the HTML page.
  • ng-repeat - It actually repeats the element on which it is used based on the provided list of data. In our example card view component using JSON object which contains a list of software, hotel, or product sample data.
  • $ctrl - By default, the components use $ctrl as the controller alias.

Angular Js Component

 
We know that the template acts as a blueprint for how our data should be organized and presented to the user. So the combination (template + controller) is such a common and recurring pattern. AngularJS provides an easy and concise way to combine them together into reusable and isolated entities, known as components. To create a component, we use the .component() method of an AngularJS module. We must provide the name of the component. The name of the component is in camelCase (e.g. myDataComponent), but we will use kebab-case (e.g. my-data-component) when referring to it in our HTML. By default, components use $ctrl as the controller alias, but we can override it, should the need arise.
 
Example:
 
Angular Components For Beginner
 

Section 1

 
Section 1 shows a short view of the code part. We create ng-app myApp and in the body part we create three ng-controller myCardHotel,myCardCompany and myCardProduct. Both ng-app and ng-controller as the two main parts of angular js. Another in header tag we include angular.min.js and link bootstrap.min.css to run angular js functionality and design css. Also, we added a link of card view style, mycomponent.css. In this style, sheet has created a component theme in which the class name is company_card,hotel_card and product_card. These names are used dynamically to show stylish output of card by using json data, and the key name is card_theme. In HTML we add code to get angular app by using angular.module() function so we can access controller by using "appName".controller() function. To create a component, use the function "appName".component(). Function .component() takes the name of component, template and controller.
  1. <html ng-app="myApp">
  2. <head>  
  3.     <link href="bootstrap.min.css" rel="stylesheet" />  
  4.     <!--link style for card view template-->  
  5.     <link href="mycomponent.css" rel="stylesheet" />      
  6.     <script src="angular.min.js"></script>  
  7.     <title>My Data Table</title>  
  8.     <script></script> <!-- program script bellow -->  
  9. <head>  
  1. var dinfoType=0;// global variable to update data info type.  
  2.      var myApp = angular.module('myApp', []);  
  3.      myApp.component('dinfoList', {  // element name will be '<dinfo-list>'.  
  4.         template:'<ul>'+  
  5.                 '<li ng-repeat="dinfo in $ctrl.datainfos">' +  
  6.                 '<div class="{{dinfo.card_theme}}">'+  
  7.                 '<div class="card">'+  
  8.                 '<div class="cardin">'+  
  9.                 '<h4>***** {{dinfo.mtitle}} *****</h4>'+  
  10.                 '<h2>{{dinfo.title}}</h2>'+  
  11.                 '<p class="title">{{dinfo.info1}}</p>'+  
  12.                 '<p>{{dinfo.info2}}</p>'+  
  13.                 '<p>{{dinfo.info3}}</p>'+  
  14.                 '<h5>* * * * *</h5>'+  
  15.                 '</div>'+  
  16.                 '</div></div></li></ul>',              
  17.         controller: function DataInfoController() {  
  18.             // calling outer function not related to controller.  
  19.            this.datainfos =MyDataInfo();  
  20.         }  
  21.     });  
  22.     // function not related to controller.  
  23.     MyDataInfo = function () {  
  24.            return datainfos;  
  25.         }           
  26.      // update data info type in controller one  
  27.      myApp.controller('myCardCompany',function($scope){  
  28.         dinfoType=1;              
  29.      });  
  30.      // update info type in controller one  
  31.      myApp.controller('myCardHotel',function($scope){  
  32.         dinfoType=2;              
  33.     });  
  34.     // update info type in controller one  
  35.     myApp.controller('myCardProduct',function($scope){  
  36.         dinfoType=3;              
  37.     });  

Section 2

 
In our code create one variable, named datainfoType and a function MyDataInfo(). Both are not related to the controller because this is common for an Angular app. In MyDataInfo() function, we load the default JSON data list. Also, it views change by updating the datainfoType variable. In JSON data one key field card_theme is important because of the runtime decision to select a theme for the card.
  1. // function not related to controller.  
  2. MyDataInfo = function () {  
  3.     if (dinfoType == 1) {  
  4.         this.datainfos = [{  
  5.                 mtitle: 'Software', title: 'Rakesh Sharma', info1: 'Project Manager', info2: 'IT Company', info3: 'rsharma@itcompany.com', card_theme:'company_card'  
  6.             }, {  
  7.                 mtitle: 'Software', title: 'Naveen Jain', info1: 'Software Developer', info2: 'IT Company', info3: 'njain@itcompany.com', card_theme:'company_card'  
  8.             }, {  
  9.                 mtitle: 'Software', title: 'Priya Mehta', info1: 'Jr. Developer', info2: 'IT Company', info3: 'pmehta@itcompany.com', card_theme:'company_card'  
  10.             } ]  
  11.         return datainfos;  
  12.     } else  
  13.         if (dinfoType == 2) {  
  14.             this.datainfos = [  
  15.                 {  
  16.                     mtitle: 'Hotel', title: 'Hotel Taj Villa', info1: 'New City, India', info2: 'htv@newcity.com', info3: '+910584245863', card_theme:'hotel_card'  
  17.                 }, {  
  18.                     mtitle: 'Hotel', title: 'Hotel City Take', info1: 'Mid City, India', info2: 'hct@midcity.com', info3: '+916525458565', card_theme:'hotel_card'  
  19.                 }, {  
  20.                     mtitle: 'Hotel', title: 'Hotel 3 Star', info1: 'Old City,India', info2: 'h2s@oldcity.com', info3: '+91325654585', card_theme:'hotel_card'  
  21.                 }  
  22.             ]  
  23.             return datainfos;  
  24.         } else  
  25.             if (dinfoType == 3) {  
  26.                 this.datainfos = [  
  27.                     {  
  28.                         mtitle: 'Product', title: 'Rolex Parfume', info1: 'Vaporisateur Natural Spray', info2: 'e 100 ml, 80% vol., 3.4 fl.oz.', info3: 'To buy: product@parfume.com', card_theme:'product_card'  
  29.                     }, {  
  30.                         mtitle: 'Product', title: 'Only Dark Parfume', info1: 'Vaporisateur Natural Spray', info2: 'e 100 ml, 80% vol., 3.4 fl.oz.', info3: 'To buy: product@parfume.com', card_theme:'product_card'  
  31.                     }, {  
  32.                         mtitle: 'Product', title: 'Dark Snake Parfume', info1: 'Vaporisateur Natural Spray', info2: 'e 50 ml, 80% vol., 1.5 fl.oz.', info3: 'To buy: product@parfume.com', card_theme:'product_card'  
  33.                     }  
  34.                 ]  
  35.                 return datainfos;  
  36.             } else  
  37.                 this.datainfos = [{ mtitle: '', title: '', info1: '', info2: '', info3: '' }]  
  38.     return datainfos;  
  39.     }    

Section 3 

Section 3 is the HTML code part. Two div are available in this section. First div tag to show page header title, second div tag shows tag shows table part. In second div tag we using component dinfo-list in three column with three different ng-controller which name is myCardHotel, myCardCompany and myCardProduct, shows in code below. So we have seen no need to repeat same code again and again to show data. Only just we write one component and we can use in our program. It depends on program requirements and how many component we need to create for our application.
  1. <body>    
  2.  <div class="container"><h2><u>Angular Js Components for Beginner</u></h2></div>       
  3.     <br/>    
  4.         <div class="container">    
  5.                 <div class="row">    
  6.                     <table class="table table-condensed">    
  7.                         <tr>    
  8.                             <!--Three controller added in table column to call component <dinfo list> -->    
  9.                             <td ng-controller="myCardHotel"><dinfo-list></dinfo-list></td>       
  10.                             <td ng-controller="myCardCompany"><dinfo-list></dinfo-list></td>    
  11.                             <td ng-controller="myCardProduct"><dinfo-list></dinfo-list></th>    
  12.                         </tr>              
  13.                     </table>                
  14.                 </div>                
  15.         </div>    
  16.             
  17. </body>    

Conclusion

 
This article shows and explains to a beginner how to create a reusable component by using angular js. Also, it tries to explain component binding dynamically. Finally, it also explains how to use dynamic theme logic in the component.