Git Product home page Git Product logo

angular-what-is-a-directive-readme's Introduction

What is a Directive?

Overview

You might not have noticed, but we've used directives already in our previous labs. Directives are essential to learning Angular, so hold on tight!

Objectives

  • Describe what Directives are

Directives, deep down

Directives are essentially "markers" on a HTML element that tell Angular to attach a specific behavior to the HTML element - either something small like a click event, or actually completely transforming the DOM node to display a list of data.

Directives can be either an actual HTML element or an attribute on a HTML element.

<my-directive></my-directive>

<!-- these are the same -->

<div my-directive></div>

However, not all directives can be used in both ways - some are restricted to being just an attribute or just an element. All of the built-in directives from Angular are restricted to being used as attributes. For any other ones (such as custom directives you download online) you will have to check the documentation on how to use it.

Types of directives

There are two types of directives - event and behavioral. Event directives handle all of the events on a HTML element we might need - for example, click, mouseover, keydown, etc. These are the same as normal JavaScript events that we would add event listeners for.

Behavioral directives are directives that manipulate the DOM. For example, we might have a list that repeats our DOM node for every item in a list. We could even have a directive for hiding and showing content depending on one of our variables' value.

We'll have more on these in the next couple of lessons.

Built-in directives

Luckily for us, Angular comes with a ton of built-in directives that do what we mentioned above and more. We'll be looking into the built-in ones in the other sections of this unit. Angular's built-in directives are prefixed with ng- (you might have noticed we're already using ng-app and ng-controller - these are just directives!) ng-app tells Angular what DOM node to put our application inside of. ng-controller tells Angular to attach our controller to that DOM node so we can access the controller inside of the element. In case you hadn't guessed, the ng- stands for Angular and it's best practice NOT to use this prefix on our own directives.

View What Is A Directive? on Learn.co and start learning to code for free.

angular-what-is-a-directive-readme's People

Contributors

annjohn avatar aturkewi avatar gilmoursa avatar ipc103 avatar peterbell avatar toddmotto avatar zachnewburgh avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-what-is-a-directive-readme's Issues

'ng' definition

The last paragraph talks about directives being prefixed with 'ng-'. There is no mention as to what this stands for. The more imaginative students may guess it's for Angular, but it would be great if it was just pointed out (less guess work, and easier to remember).

On a similar note the variable 'vm' is used heavily in this section without introduction. This blog explains it quite nicely: http://www.johnpapa.net/angularjss-controller-as-and-the-vm-variable/

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.