Embedding Html Pages within HTML page

Last updated on May 31 2022
Santosh Prakash

Table of Contents

Embedding Html Pages within HTML page

HTML does not support embedding html pages within html page. To achieve this functionality following ways are used −

  • Using Ajax − Make a server call to get the corresponding html page and set it in innerHTML of html control.
  • Using Server Side Includes − JSP, PHP and other web side server technologies can include html pages within a dynamic page.

Using AngularJS, we can embed HTML pages within a HTML page using ng-include directive.

<div ng-app = “” ng-controller = “studentController”>

<div ng-include = “‘main.htm'”></div>

<div ng-include = “‘subjects.htm'”></div>

</div>

Example

tryAngularJS.htm

 

<html>

<head>

<title>Angular JS Includes</title>

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

</script>

 

<style>

table, th , td {

border: 1px solid grey;

border-collapse: collapse;

padding: 5px;

}

 

table tr:nth-child(odd) {

background-color: #f2f2f2;

}

 

table tr:nth-child(even) {

background-color: #ffffff;

}

</style>

</head>

 

<body>

<h2>AngularJS Sample Application</h2>

 

<div ng-app = “mainApp” ng-controller = “studentController”>

<div ng-include = “‘/angularjs/src/include/main.htm'”></div>

<div ng-include = “‘/angularjs/src/include/subjects.htm'”></div>

</div>

 

<script>

var mainApp = angular.module(“mainApp”, []);

 

mainApp.controller(‘studentController’, function($scope) {

$scope.student = {

firstName: “Mahesh”,

lastName: “Parashar”,

fees:500,

 

subjects:[

{name:’Physics’,marks:70},

{name:’Chemistry’,marks:80},

{name:’Math’,marks:65},

{name:’English’,marks:75},

{name:’Hindi’,marks:67}

],

 

fullName: function() {

var studentObject;

studentObject = $scope.student;

return studentObject.firstName + ” ” + studentObject.lastName;

}

};

});

</script>

 

</body>

</html>

main.htm

<table border = “0”>

<tr>

<td>Enter first name:</td>

<td><input type = “text” ng-model = “student.firstName”></td>

</tr>

 

<tr>

<td>Enter last name: </td>

<td><input type = “text” ng-model = “student.lastName”></td>

</tr>

 

<tr>

<td>Name: </td>

<td>{{student.fullName()}}</td>

</tr>

</table>

subjects.htm

<p>Subjects:</p>

<table>

<tr>

<th>Name</th>

<th>Marks</th>

</tr>

 

<tr ng-repeat = “subject in student.subjects”>

<td>{{ subject.name }}</td>

<td>{{ subject.marks }}</td>

</tr>

</table>

Output

To run this example, you need to deploy textAngularJS.htm, main.htm and subjects.htm to a webserver. Open textAngularJS.htm using url of your server in a web browser. See the result.

 

 

AngularJS – Ajax

AngularJS provides $https: control which works as a service to read data from the server. The server makes a database call to get the desired records. AngularJS needs data in JSON format. Once the data is ready, $https: can be used to get the data from server in the following manner −

function studentController($scope,$https:) {

var url = “data.txt”;

 

$https:.get(url).success( function(response) {

$scope.students = response;

});

}

Here, the file data.txt contains student records. $https: service makes an ajax call and sets response to its property students. students model can be used to draw tables in HTML.

Examples

data.txt

[

{

“Name” : “Mahesh Parashar”,

“RollNo” : 101,

“Percentage” : “80%”

},

 

{

“Name” : “Dinkar Kad”,

“RollNo” : 201,

“Percentage” : “70%”

},

 

{

“Name” : “Robert”,

“RollNo” : 191,

“Percentage” : “75%”

},

 

{

“Name” : “Julian Joe”,

“RollNo” : 111,

“Percentage” : “77%”

}

]

testAngularJS.htm

 

<html>

<head>

<title>Angular JS Includes</title>

 

<style>

table, th , td {

border: 1px solid grey;

border-collapse: collapse;

padding: 5px;

}

 

table tr:nth-child(odd) {

background-color: #f2f2f2;

}

 

table tr:nth-child(even) {

background-color: #ffffff;

}

</style>

</head>

 

<body>

<h2>AngularJS Sample Application</h2>

<div ng-app = “” ng-controller = “studentController”>

 

<table>

<tr>

<th>Name</th>

<th>Roll No</th>

<th>Percentage</th>

</tr>

 

<tr ng-repeat = “student in students”>

<td>{{ student.Name }}</td>

<td>{{ student.RollNo }}</td>

<td>{{ student.Percentage }}</td>

</tr>

</table>

</div>

 

<script>

function studentController($scope,$http) {

var url = “data.txt”;

 

$http.get(url).then( function(response) {

$scope.students = response.data;

});

}

</script>

 

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

</script>

 

</body>

</html>

Output

To execute this example, you need to deploy testAngularJS.htm and data.txt file to a web server. Open the file testAngularJS.htm using the URL of your server in a web browser and see the result.

So, this brings us to the end of blog. This Tecklearn ‘Embedding Html Pages within HTML page’ 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 "Embedding Html Pages within HTML page"

Leave a Message

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