I'm going to go ahead and take that back out because the next example is using this slot. This selector is using a CSS selector that says the title attribute should match select me. And in my parent component, I am passing a paragraph tag that has a title attribute with the value select me. So, even though we're passing it at the top, it should end up at the bottom, which it does.
If I didn't have that slot selector and it just randomly went into the default slot, then we'd have select me in the club with the people and we don't want that. No, no. Just like view, we can also do conditional content projection. But this uses the ng-template tag. It's based on the template element instead. So, this one's going to be a little bit more of a journey that we're going on together.
As a prop, I am passing the string for the source for my image to where I'm getting the people to put in the club. And I have my conditional content projection component. It's taking people in as a prop and creating refs to these ng-template tags. Right now I have a little john-ng-template and that I'm slotting right into an ng-container tag. So, I'm using the ng-template outlet attribute to grab that template tag and mount it inside this container. So, we should see people in the club. We should see a confused little john, which we do.
If we want to see people in the club, then we're going to use this ng-content container that is accessing the people in the club ref. This time I'm using a bound attribute so that I can pass some context to the container, which is the string of where to get the people to put in the club. Then I'm passing that as another bound attribute to my template tag, which then finally passes it to my image. So, we should see people in the club, which we do. I've created a component that does just that. Right now the prop showPeople is set to false.
If we go up here, I've got my same two ng-template tags and this time on ng-init, which is after I get all my props and everything, I say, hey, is showPeople true or false? Right now it's false, so we're taking a view container and creating an embedded view and passing the Lil Jon ng-template tag. This is going to do the exact same thing as having the ng-container tag and using the ng-template outlet attribute. We should see Confuse Lil Jon. If we want to see people in the club, then we set showPeople to true.
Now, view container createEmbeddedView will pass both the template tag and then people as context as well. There we go. But wait, there's more. Angular said, what if we allowed you to have a view encapsulation mode that's literally the Shadow DOM API? I have one final component called viewEncapsulationMode. When I define my component, I use the encapsulation property to select viewEncapsulationShadowDOM. We should see Lil Jon's graduation photo, or Lil Lil Jon, if you will, inside a shadow root exactly like we would have seen in web components. Here's his graduation photo and there it is inside a shadow root. Start thinking with slots.
Here's a QR code to get these slides. There's ways to contact me, all of the repos that I've been using as examples, and the blog with all of this information. Hope you have a great time using slots.
Comments