MonkeyBonkey
MonkeyBonkey

Reputation: 47881

In Angular how do you pass the innerText or innerHtml to a component

I see from the docs that you can use @input to pass a property value to the component.

However how can you pass the innerText to the component so that one can do something like this?

<error-label>this is required"</error-label>

and pass the inner text to the component.

Upvotes: 2

Views: 1573

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657406

You can use a tempalte variable to create a reference and access it from within the template

<div #myDiv>foo</div>
<button (click)="sendInnerText(myDiv.innerText)">pass inner text</div>

or the component

@ViewChild('myDiv') divRef:ElementRef;

ngAfterViewInit() {
  console.log(this.div.nativeElement.innerText);
}

Upvotes: 2

Related Questions