Using Directives and Expressions in Angular JS

Last updated on May 31 2022
Santosh Prakash

Table of Contents

Using Directives and Expressions in Angular JS

AngularJS directives are used to extend HTML. These are special attributes starting with ng- prefix. We’re going to discuss following directives −

  • ng-app − This directive starts an AngularJS Application.
  • ng-init − This directive initializes application data.
  • ng-model − This directive binds the values of AngularJS application data to HTML input controls.
  • ng-repeat − This directive repeats html elements for each item in a collection.

ng-app directive

ng-app directive starts an AngularJS Application. It defines the root element. It automatically initializes or bootstraps the application when web page containing AngularJS Application is loaded. It is also used to load various AngularJS modules in AngularJS Application. In following example, we’ve defined a default AngularJS application using ng-app attribute of a div element.

<div ng-app = “”>

</div>

ng-init directive

ng-init directive initializes an AngularJS Application data. It is used to put values to the variables to be used in the application. In following example, we’ll initialize an array of countries. We’re using JSON syntax to define array of countries.

<div ng-app = “” ng-init = “countries = [{locale:’en-US’,name:’United States’},

{locale:’en-GB’,name:’United Kingdom’}, {locale:’en-FR’,name:’France’}]”>

</div>

ng-model directive

This directive binds the values of AngularJS application data to HTML input controls. In following example, we’ve defined a model named “name”.

<div ng-app = “”>

<p>Enter your Name: <input type = “text” ng-model = “name”></p>

</div>

ng-repeat directive

ng-repeat directive repeats html elements for each item in a collection. In following example, we’ve iterated over array of countries.

<div ng-app = “”>

<p>List of Countries with locale:</p>

 

<ol>

<li ng-repeat = “country in countries”>

{{ ‘Country: ‘ + country.name + ‘, Locale: ‘ + country.locale }}

</li>

</ol>

</div>

Example

Following example will showcase all the above mentioned directives.

testAngularJS.htm

 

<html>

<head>

<title>AngularJS Directives</title>

</head>

 

<body>

<h1>Sample Application</h1>

 

<div ng-app = “” ng-init = “countries = [{locale:’en-US’,name:’United States’},

{locale:’en-GB’,name:’United Kingdom’}, {locale:’en-FR’,name:’France’}]”>

<p>Enter your Name: <input type = “text” ng-model = “name”></p>

<p>Hello <span ng-bind = “name”></span>!</p>

<p>List of Countries with locale:</p>

 

<ol>

<li ng-repeat = “country in countries”>

{{ ‘Country: ‘ + country.name + ‘, Locale: ‘ + country.locale }}

</li>

</ol>

</div>

 

<script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”>

</script>

 

</body>

</html>

Output

Open textAngularJS.htm in a web browser. Enter your name and see the result.

 

 

AngularJS – Expressions

Expressions are used to bind application data to html. Expressions are written inside double braces like {{ expression}}. Expressions behaves in same way as ng-bind directives. AngularJS application expressions are pure javascript expressions and outputs the data where they are used.

Using numbers

<p>Expense on Books : {{cost * quantity}} Rs</p>

Using strings

<p>Hello {{student.firstname + ” ” + student.lastname}}!</p>

Using object

<p>Roll No: {{student.rollno}}</p>

Using array

<p>Marks(Math): {{marks[3]}}</p>

Example

Following example will showcase all the above mentioned expressions.

testAngularJS.htm

<html>

<head>

<title>AngularJS Expressions</title>

</head>

 

<body>

<h1>Sample Application</h1>

 

<div ng-app = “” ng-init = “quantity = 1;cost = 30;

student = {firstname:’Mahesh’,lastname:’Parashar’,rollno:101};

marks = [80,90,75,73,60]”>

<p>Hello {{student.firstname + ” ” + student.lastname}}!</p>

<p>Expense on Books : {{cost * quantity}} Rs</p>

<p>Roll No: {{student.rollno}}</p>

<p>Marks(Math): {{marks[3]}}</p>

</div>

 

<script src = “https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js”>

</script>

 

</body>

</html>

Output

Open textAngularJS.htm in a web browser. See the result.

So, this brings us to the end of blog. This Tecklearn ‘Using Directives and Expressions in Angular JS’ blog helps you with commonly asked questions if you are looking out for a job in Angular JS and Front-End Development. If you wish to learn Angular JS and build a career in Front-End Development domain, then check out our interactive, Angular JS Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/angular-js-training/

Angular JS Training

About the Course

Angular JS certification training is designed for developers who are responsible for building applications using AngularJS. Through a combination of presentations and hands-on practices, participants will explore Angular comprehensively and learn to build components, directives, template-driven forms, routing, etc. for complex data-centric (enterprise) applications. This Angular online training will update your knowledge and skills with the current Angular version to lift your career.

Why Should you take Angular JS Training?

  • The average salary for “Angular Developer” ranges from approximately $92,814 per year for a Developer to $113,069 per year for a Full Stack Developer (Indeed.com).
  • YouTube, Google, Cisco, Nike, Samsung, Microsoft, Forbes, BMW and many Fortune 500 companies are using Angular as their front-end development framework to deliver an engaging user experience.
  • Angular’s powerful cross-platform feature allows developers to build progressive web applications and native mobile applications.

What you will Learn in this Course?

Introduction to Angular

  • Introduction to Angular
  • Comparison between front-end tools
  • Angular Architecture
  • Building blocks of Angular
  • Angular Installation
  • Angular CLI
  • Angular CLI commands
  • Angular Modules

Angular Components and Data Binding

  • Working of Angular Applications
  • Angular App Bootstrapping
  • Angular Components
  • Creating A Component Through Angular CLI
  • Ways to specify selectors
  • Template and styles
  • Installing bootstrap to design application
  • Data Binding
  • Types of Data Binding
  • Component Interaction using @Input and @Output decorator
  • Angular Animations

TypeScript

  • What is TypeScript
  • Need of TypeScript
  • How to install TypeScript
  • Nodemon for monitoring changes
  • Interfaces in Class, String Templates, Maps,
  • Sets and Object Restructuring

Directives and Pipes in Angular

  • Angular Directives
  • @Component Directive
  • Structural Directives
  • Attribute Directives
  • Custom Directives
  • Pipes
  • Built-in Pipes
  • Chaining pipes
  • Custom pipes
  • Pipe Transform Interface & Transform Function
  • Pure and Impure pipes

Angular Services and Dependency Injection

  • Angular service
  • Need for a service
  • Dependency Injection
  • Creating a service
  • Hierarchical Injector
  • Injecting A Service into Another Service
  • Observables
  • RxJS Library
  • Angular’s Interaction with Backend
  • Parts of an Http Request
  • HttpClient

Forms in Angular

  • Forms in Angular
  • What are their functions
  • Advantages of Forms
  • Various Types of Forms
  • What is Angular Validation and Model driven approach

Angular Routing

  • Angular Routing
  • Angular Routing benefits and features
  • Building a single page application and updating it dynamically with Angular Routing
  • What is Parameter Routing
  • Router Lifecycle Hooks and Child Routes

Authentication and Security in Angular

  • What is Authentication?
  • Authentication and authorization
  • Types of Authentication
  • Where to store tokens?
  • JSON Web Tokens (JWT)
  • Authentication in Angular application
  • Security threats in web application

Testing Angular applications

  • Testing of Angular applications
  • Deployment of Angular Test Bed for testing on Angular framework
  • Setup of various tools for testing
  • Testing services in Angular Framework
  • E2E and Document Object Model testing

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Using Directives and Expressions in Angular JS"

Leave a Message

Your email address will not be published. Required fields are marked *