How can I insert one html page into another html page using a custom directive in AngularJS?
Using custom directives I want to add one HTML page to another, how can this be done? The custom directive looks like this:
(this is the .js file)
fbModule.directive('fbLogin', function(){
return {
template : html
}
})
And the html page to be included:
(this is the .html file to be included)
<div ng-controller="fbCtrl">
<button ng-click="doFacebookLogin">
<img src="modules/shippingAddress/fastfill_fb.png" width="140px" height="25px;" style="margin-right: 8px; cursor:pointer">
</button>
</div>
The page within which the above code should be listed:
(this is the .html file in which the above html should come)
<div fbLogin></div>
<div style="font-size: x-small; color: black;">Save on typing. Use your FB data.</div>
Please help .. Thanks in advance
source to share
you need to change the template to templateUrl if you need to get external html like below.
templateUrl can also be a function that returns the URL of the HTML template to download and use for this directive. Angular will call the templateUrl function with two parameters: the element that the directive was called and the attr object associated with that element.
fbModule.directive('fbLogin', function() {
return {
template: 'template.html'
};
});
to
fbModule.directive('fbLogin', function() {
return {
templateUrl: 'template.html'
};
});
source to share
You have to use ngInclude to inject HTML into another page https://docs.angularjs.org/api/ng/directive/ngInclude
<ng-include src="somehtmlfile.html"></ng-include>
source to share
Basically:
fbModule.directive('fbLogin', function(){
return {
templateUrl : "...path to html"
}});
... but read the Developer Guide https://docs.angularjs.org/guide/directive
source to share
If you want to completely replace the directive tag with the html file loaded by the directive, you must use templateUrl: 'path/to/file.html'
and replace: true
so that your directive tag is replaced by the html instead of being included in it. Here's an example of a directive:
app.directive('fbLogin', function(){
return {
templateUrl : 'include.html',
replace: true
}
});
source to share