I’m posting this here because it took me a while to figure out how to do this and hopefully I’ll learn a better way of doing it.
I wanted to get a custom elements data that would get inserted into a <slot></slot> into a variable that I could play around with.
For example, we have a pattern library and we want to show code samples. I want a custom element where I can put sample code in side the custom element and have the component render the code, not the html.
In my first attempt I was able to get something working by modifying the content with the @processContent decorator.
But by getting the content into a variable that I could work with in my view model, I can do more with it, such toggle between rendering the code or rendering the html.
Why not inject the element into your view-model class, and then grab its inner HTML?
import {inject} from 'aurelia-framework';
@inject(Element)
export class toggleCode {
constructor(element) {
this.innerHTML = element.innerHTML;
}
}
And if you want to be sure Aurelia does not process the code inside the toggle-code element, just add a @processContent(false) decorator on the view-model class.
This is an interesting idea. I noticed that it wrapped the content with <au-content> I would just have to pull that out. Also, it seem to “fix” html for you. Like if I do a simple table with no <tbody> tag, it will throw the <tbody> tag in there for me.
Well, I thought this would work, but the problem is my code sample has aurelia components. they get rendered into their html with this method. So the code samples don’t work.