Git Product home page Git Product logo

angular-restricting-directive-usage-readme's Introduction

Restricting Directive usage

Overview

We've learned that directives can be used as HTML attributes and HTML elements, but did you know that you can tell Angular to only allow your directive to be used in a certain way?

Objectives

  • Describe the syntaxes for declaring Directives
  • Restrict our Directive
  • Why we restrict our directives

Restrict

Angular allows us to set a property named restrict on the object we return on our directive definition. We can pass through a string with certain letters letting Angular know how our directive can be used.

function MyDirective() {
	return {
		restrict: 'E',
		template: '<div>Hello world!</div>'
	};
}

angular
	.module('app')
	.directive('myDirective', MyDirective);

Above, we've got restrict set to E. This stands for element, meaning our directive can only be used as an element.

The letters available are:

A - used as an attribute E - used as an element C - used as a class name M - used as a comment

They would be used as the following:

Attribute - <div my-directive></div>
Element - <my-directive></my-directive>
Class: <div class="my-directive"></div>
Comment - <!-- directive: my-directive -->

You might've noticed that we can use directives in class names and comments too. This isn't recommended however, as it is unclear if we're just doing a normal HTML comment or invoking a directive. It also isn't clear if we're adding a class to an element or invoking a directive too.

You can use the individual letters or put them together - 'EA' will allow the directive to be used as an element and an attribute.

By default, restrict is set to 'EA'.

View Restricting Directive Usage on Learn.co and start learning to code for free.

View Restricting Directive Usage on Learn.co and start learning to code for free.

angular-restricting-directive-usage-readme's People

Contributors

annjohn avatar coffeeexistence avatar pletcher avatar toddmotto avatar

Watchers

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

angular-restricting-directive-usage-readme's Issues

Add opening section

This is a great demonstration of the implementation of restricting directives. The reader will need to see the problems associated with NOT restricting directive usage first. Add an opening section that demos the different ways to use custom directives and then describes the problem associated with using directives different ways. Then, the "Restrict" section will be showing the solution to this problem.

'cc @toddmotto

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.