Menu

Monday, 12 January 2015

AngularJS Core Directives: Include, Click and Repeat

About

This article provides an idea of some more directives like ng-include, ng-click and ng-repeat in AngularJs.
This article is just a continuation of my previous article about Quick Start Of AngularJS. So, please read that to get the initial/basic idea of AngularJS.

ng-include

This is an AngularJS directive, it is very helpful to include the various files in a main page using the ng-include attribute.
For example, you have one page layout that contains a header, body, footer and so on. In that scenario, you can create various HTML files for a header and footer then you can include those in the main HTML file. Because of this implementation the page looks cleaner and the code/design is also separated.
Sample Application
The header.html file contains the following.

Header of the site

The footer HTML file contains the following.

Copyright Ramchand @2014

The main index.html file contains the following.  



The previous index.html contains the ng-include attributes for header and footer divs with the respective HTML file path values that should be specified in single quotes.
Now, when you run the index.html file, the output looks as in the following.



You can run the application using Plunker tool at  AngularJS include.

ng-click

This is also one of the directives, you can use this in one of the scenarios like when you click on a button if you do any operation then this would be useful.
Scenario: The form contains an input text box and Search button, whenever the user enters a value into a text box and clicks on the search button you need to display the user-entered value, if the user clicks on the search button without entering anything then we need to display a message.

The index.html file looks as in the following. 



 The DemoClick.js file contains the following lines of JavaScript code. 

(function() {  
  var app = angular.module("DemoClickApp", []);  
  
  var DemoController = function($scope) {  
    $scope.Search = function(empname) {  
      if (empname)  
        $scope.result = "You have searched for " + empname;  
      else  
        $scope.result = "Please enter employee name";  
    };  
  };  
  app.controller("DemoController", DemoController);  
})();  

 Now, when you run the index.html file by default it looks as in the following.



If you click on the Search button without entering any value in the text box then the output screen displays an alert kind of message as "Please enter employee name" that looks as in the following.

 Now, you can enter some value into a text box and click on the search box and then the displayed screen is as follows.


You can run the previous discussed example using the Plunker tool at AngularJS click.

ng-repeat
 
This directive is like a foreach loop in C#. By using this directive you can display a collection of items in a view (HTML page).
Scenario: You can display a list of employees using the ng-repeat directive in AngularJS.
  
  
  
  
    
    
    
    
  
  
  
  
  

List of Emplooyees

Name Designation Location
{{emp.Name}} {{emp.Designation}} {{emp.Location}}
The previous HTML file is self explanatory since it contains point-by-point details in comments format. The fifth point is that you have added something as a ng-repeat attribute with the value of "emp in employees". Here "employees" is the collection value that is obtained from the Employee.JS controller.
The Employee.js contains the following lines of JavaScript code.
(function() {  
  var app = angular.module("DemoRepeatApp", []);  
  
  var DemoController = function($scope) {  
    var sampleEmployees = '[' +  
      '{ "Name":"Ramchand" , "Designation":"SSE" , "Location":"Bhubhaneswar" },' +  
      '{ "Name":"Lakshman" , "Designation":"DBA" , "Location":"Noida" },' +  
      '{ "Name":"ABC" , "Designation":"Team Lead" , "Location":"Banglore" } ]';  
    $scope.employees = JSON.parse(sampleEmployees);  
  };  
  app.controller("DemoController", DemoController);  
})();

The following describes the Employee.js file: 
  1. Angular module created with the name DemoRepeatApp.

  2. The variable DemoController is assigned for the Controller action.

  3. The Controller contains a variable named "sampleEmployees" that contains a list of sample employess in JSON format.

  4. Assign the JSON converted sample employees list to the $scope.employees object. 
Now, you can run the index.html file and then the output will be displayed as in the following.

You can run the previous example using Plunker at AngularJS Repeat .
You can download the aforesaid examples at AngularJS Directives

Conclusion

This article provided an idea of core directives of ng-include, ng-click and ng-repeat in AngularJS.

8 comments:

  1. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information about the web design and web development.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    Hire Angularjs Developer

    ReplyDelete
  2. It is nice blog Thank you provide important information and i am searching for same information to save my time AngularJS5 Online Training Hyderabad

    ReplyDelete
  3. awesome blog it's very nice and useful i got many more information on angular certification

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your blog. For the similar kind of information FollowCodersNewsThey also provideTop 10 AngularJS Development Companies

    ReplyDelete