![]() |
VOOZH | about |
The AngularJS $templateRequest service is used to fetch the contents of an HTML template. It makes an HTTP GET request to the specified template URL and returns the template content as a string. The $templateRequest service is part of the ngRoute module, which must be included in your application in order to use this service.
The $templateRequest service returns a promise that is resolved with the template content when the HTTP GET request succeeds, or is rejected with an error if the request fails. The $templateRequest service is useful for loading templates dynamically, for example when using AngularJS routing to load different templates for different routes in a single-page application.
Syntax: It illustrates the basic use of the $templateRequest service:
angular.module('myApp', [])
.controller('MyController', ['$templateRequest', function($templateRequest) {
$templateRequest('template.html',options).then(function(template) {
// do something with the template
});
}]);
Parameters: The AngularJS $templateRequest service accepts the following arguments:
Return type: A promise for the HTTP response data will be returned for the given URL.
Example 1: This HTML file contains an AngularJS application that uses the $templateRequest service to retrieve the contents of an HTML template file. When the page is loaded, the $templateRequest service makes an HTTP GET request to the specified template URL (in this case, "GFG1.html"). If the request succeeds, the template content is written to the page using the document.write function.
Output:
Example 2: This is another example that illustrates the implementation of the AngularJS $templateRequest service by creating different templates & trying to retrieve the contents of an HTML template file by using the $templateRequest service.
Output:
Reference: https://docs.angularjs.org/api/ng/service/$templateRequest