0 votes
in Angular by
How Can We Create a Custom Directive in Angular?

1 Answer

0 votes
by

Till now, we have looked in to predefined Angular directives like ng-controllerng-model and so on. But what if we want to create our own custom Angular directive and attach it with HTML elements as shown in the below code.

HTML
<div id=footercompany-copy-right></div>

To create a custom directive, we need to use the directive function to register the directive with Angular application. When we call the register method of directive, we need to specify the function which will provide the logic for that directive.

For example, in the below code, we have created a copyright directive and it returns a copyright text.

Please note app is an angular application object which has been explained in the previous sections.

JavaScript
app.directive('companyCopyRight', function ()
{
return
{
        template: '@CopyRight questpond.com '
 };
});

The above custom directive can be later used in elements as shown in below code.

HTML
<div ng-controller="CustomerViewModel">
<div company-copy-right></div>
</div>
...