Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text from div and pass to the function

Tags:

angular

I'm trying to pass div text in function using angular2

I have this in my template

<div (click)="logThis(this.text)">test</div>

and in component

logThis(x: any){
        console.log(x);
}

If I'll put some text instead of this.text it logs without any problem

I've tried this.value as well but it logs undefined

Other I've tried to add <div #test (click)="logThis(test.text)">test</div> but same result

like image 805
gsiradze Avatar asked Dec 25 '22 00:12

gsiradze


1 Answers

You can't use this in the template of a component. Your template variable approach is correct though but I think there is no text property:

<div (click)="logThis(div.innerText)" #div>test</div>
<div (click)="logThis(div.textContent)" #div>test</div>
like image 51
Günter Zöchbauer Avatar answered Dec 28 '22 11:12

Günter Zöchbauer