Thursday, November 5, 2015

AngularJS - What are the different custom directive types in AngularJS?

Ref:- http://www.codeproject.com/Articles/891718/AngularJS-Interview-Questions-and-Answers#Whatarecontrollersandneedofng-controllerandng-modelinAngular

There are different flavors of Angular directives depending till what level you want to restrict your custom directive.
In other words do you want your custom directive to be applied only on HTML element or only on an attribute or just to CSS etc.
So in all there are four different kinds of custom directives:-
  • Element directives (E)
  • Attribute directives (A)
  • CSS class directives (C)
  • Comment directives (M)
Below is a simple custom directive implementation at the element level.
myapp.directive('userinfo', function()
{
    var directive = {};
    directive.restrict = 'E';
    directive.template = "User : {{user.firstName}} {{user.lastName}}";
    return directie;
});
The “restrict” property is set to “E” which means that this directive can only be used at element level as shown in the code snippet below.
<userinfo></userinfo>
If you try to use it at an attribute level as shown in the below code it will not work.
<div userinfo></div>
So “E” for element, “A” for attribute, “C” for CSS and “M” for comments.

What if I want custom directives to be applied on element as well as attributes ?

directive.restrict = 'EA';

Can I set an Angular directive template to a HTML web page?

Yes, you can set template to page directly by using “templateUrl” property of the directive as shown in the code snippet below.
directive.templateUrl = "/templates/footer.html";

2 comments:

  1. Hi There,


    Thank you! Thank you! Thank you! Your blog was a total game changer!
    I have biggest confusion to choose my career.any help me ..
    I'm a new to asp.net mvc development.
    I have some questions regarding that

    1.Use of Entity Framework.?
    2.Why do use jquery .?
    3.AngularJS Development.?
    Can anyone suggest a couple of example.?

    Very useful article, if I run into challenges along the way, I will share them here.


    Many Thanks,
    Niha

    ReplyDelete
  2. Hi There,


    Smokin hot stuff! You’ve trimmed my dim. I feel as bright and fresh as your prolific website and blogs!

    I am developing an angular app, I have a Stored Proc using in which I have a lot of data related to multiple pages. I want to call that SP in master page only and I will save data in multiple variables related to diff-diff pages.
    On master page I want to display
    some of the data and further u have some links in master page which will navigate to respective pages. On those respective pages I don't want to call separate SP, I want to utilize data which I already got on master page.


    Once again thanks for your tutorial.


    MuchasGracias,
    Franda

    ReplyDelete