3

I would like to conditionally apply a title attribute to an element, which I am attempting like so:

<div class="option-title" [attr.title]="isTitleTruncated() ? option.title : null">
  {{option.title}}
</div>

But I would like to pass in the element reference to my function being used on the condition, but have been unable to do so. I tried using this and that gives me the component instance, but I want the actual div itself. Is this possible?

2 Answers 2

8

Try using template reference variable like:

<div ... #divRef [attr.title]="isTitleTruncated(divRef) ? option.title : null">
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this worked, although I have hit a change detection error now, which I will post a separate question on
1

You should create a template reference variable on the elmenent that looks like #myDiv and then pass it to the function.

<div class="option-title" #myDiv [attr.title]="isTitleTruncated(myDiv) ? option.title : null">
  {{option.title}}
</div>

1 Comment

what is myDiv type? it just outputs markup in console.log, if i put any type to check it out, so it's not clear what type of data is that. I need to swap CSS class for clicked element.

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.