Skip to content

Navigation Menu

Sign in
Sign up

How can I render html both in component element and out component element? #296

Answered by Scipion
qlfy17 asked this question in Q&A
Discussion options

First,define the primary-button component,like this:

@controller
class PrimaryButtonElement extends HTMLElement {
 connectedCallback(){
 const itemTemplate = () => {
 return html`
 <button>
 ???
 </button>
 `
 }
 render(itemTemplate(), this)
 }
}

Then,use the component like this,

<primary-button>new issue</primary-button>

The question is how to render the component's content in the controller?
Thanks!

You must be logged in to vote

I'm not sure of the question. Do you mean you want to wrap what the children of the tag into your button? if so you can get the initial content with this.innerHTML as a string and do it like so:

@controller
export class PrimaryButtonElement extends HTMLElement {
 connectedCallback(){
 const initialContent = this.innerHTML;
 const itemTemplate = (content:string) => {
 return html`<button>${content}???</button>`
 }
 this.innerHTML=''
 render(itemTemplate(initialContent), this);
 }
}

You will have to get rid of it and re-render it again.
it will render:

<primary-button data-catalyst="">
 <button>new issue???</button>
</primary-button>

I hop...

Replies: 2 comments

Comment options

I'm not sure of the question. Do you mean you want to wrap what the children of the tag into your button? if so you can get the initial content with this.innerHTML as a string and do it like so:

@controller
export class PrimaryButtonElement extends HTMLElement {
 connectedCallback(){
 const initialContent = this.innerHTML;
 const itemTemplate = (content:string) => {
 return html`<button>${content}???</button>`
 }
 this.innerHTML=''
 render(itemTemplate(initialContent), this);
 }
}

You will have to get rid of it and re-render it again.
it will render:

<primary-button data-catalyst="">
 <button>new issue???</button>
</primary-button>

I hope it helps.

You must be logged in to vote
0 replies
Answer selected by keithamus
Comment options

I'm not sure of the question. Do you mean you want to wrap what the children of the tag into your button? if so you can get the initial content with this.innerHTML as a string and do it like so:

@controller
export class PrimaryButtonElement extends HTMLElement {
 connectedCallback(){
 const initialContent = this.innerHTML;
 const itemTemplate = (content:string) => {
 return html`<button>${content}???</button>`
 }
 this.innerHTML=''
 render(itemTemplate(initialContent), this);
 }
}

You will have to get rid of it and re-render it again. it will render:

<primary-button data-catalyst="">
 <button>new issue???</button>
</primary-button>

I hope it helps.

thanks a lot

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #294 on October 31, 2022 16:34.

AltStyle によって変換されたページ (->オリジナル) /