3

I have issue with getting data from HTML data attr to component.

In Template I have:

<button [attr.data-direction]="next" type="text" (click)="nextNotes()" [disabled]="checkPage(paginator)"> > </button>

And how can I call this data attr in class method after click? There is some ng workflow for that or purejs?

console.log(data)
3
  • you want to get the data-direction attribute ? try with this <button #myBtn ..., then in your TS class you can retrieve the attribute with myBtn.getAttribute('data-direction') Commented Feb 7, 2017 at 15:28
  • But what type has variable myBtn? Commented Feb 7, 2017 at 15:36
  • ElementRef, you can retrieve it like this constructor(myBtn: ElementRef) { ... }, then you can try with myBtn.nativeElement.getAttribute(...) Commented Feb 7, 2017 at 15:41

1 Answer 1

3

Not sure if I am 100 % capish here, but if you just want the value from data-directive, then you could pass the button as a local variable to nextNotes() and retrieve the value from there using getAttribute

Template:

<button #btn data-direction="next" type="text" (click)="nextNotes(btn)" [disabled]="checkPage(paginator)"></button>

Class method:

nextNotes(val) {
  console.log(val.getAttribute('data-direction'))
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.