Code Monkey home page Code Monkey logo

angular-multi-slot-transclusion-readme-v-000's Introduction

Multi-slot transclusion

Overview

One other awesome thing that transclusion offers is the concept of multi-slot transclusion. This allows us to pick different elements in our transcluded content and put them wherever we like.

Objectives

  • Describe the new multi-slot transclude feature
  • Create multiple transclusion slots
  • Create named transclusion slots
  • Define optional transclusion slots

transclude: {}

Instead of a string or a boolean value, to do multi-slot transclusion, we pass through an object.

We might use multi-slot transclusion when we want to move certain elements into different positions in our directives templates, instead of just putting them all in one place. We might want to wrap an image with a link or a user profile with an image.

The properties on this object are what we want to name our slots. For instance, if we've got this setup for our directive:

<user-profile>
    <h4>Tim Cook</h4>
    <h6>CEO, Apple</h6>
</user-profile>

You can see here that our <h4> element is the name and our <h6> is the position. Therefore, it makes sense to name our slots name and position.

We then put the values of our properties to be the element that we're targeting. We'd end up with something like this:

function UserProfile() {
  return {
    transclude: {
        name: 'h4',
        position: 'h6'
    },
    template: [
      '<div class="user">',
        '<h2>User Profile</h2>',
      '</div>'
    ].join('')
  };
}

angular
  .module('app', [])
  .directive('userProfile', UserProfile);

We can now access these elements using the familiar ng-transclude directive. We pass in the slot name that we want as the value.

function UserProfile() {
  return {
    transclude: {
        name: 'h4',
        position: 'h6'
    },
    template: [
      '<div class="user">',
        '<h2>User Profile</h2>',
        '<div>Name: <span ng-transclude="name"></span></div>',
        '<div>Position: <span ng-transclude="position"></span></div>',
      '</div>'
    ].join('')
  };
}

angular
  .module('app', [])
  .directive('userProfile', UserProfile);

You can see how we can put our slots anywhere we like in our directive. Also, we don't have to match the tags that we transclude - you can see that although we've picked out <h4> and <h6> we're transcluding them inside of a <span /> element (this does put our header elements inside our span element, not just the text).

Optional slots

Now, if you remove either the <h4> or <h6>, you will get an error as we're expecting there to be both of them.

We can mark slots as optional, too. Let's make position optional, as everyone has a name but people might not have a position. To make a slot as optional, inside of our string we add a ? before the element.

function UserProfile() {
  return {
    transclude: {
        name: 'h4',
        position: '?h6'
    },
    template: [
      '<div class="user">',
        '<h2>User Profile</h2>',
        '<div>Name: <span ng-transclude="name"></span></div>',
        '<div>Position: <span ng-transclude="position">No position</span></div>',
      '</div>'
    ].join('')
  };
}

angular
  .module('app', [])
  .directive('userProfile', UserProfile);

Now we use our directive with and without specifying a <h6> element. If there isn't a <h6> element, the contents of our <span ng-transclude="position"> will be displayed instead, meaning we can gracefully show different content if there is no position specified.

View Angular Multi-slot Transclusion on Learn.co and start learning to code for free.

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.