1

i have an object like this var obj = {"test price type ty dynamic ": 10, test: 7, pricetype1u: 0, Price type 3: 0, Price type 2: 0}

in an angular component html, using bootstrap i written like this

<button type="button" 
        icon="file-excel" 
        data-toggle="tooltip" 
        data-placement="top"
        title="{{obj}}">

        <span class="k-icon k-i-info"></span>
</button>

tooltip is showing like (object) this:

{
  "test price type ty dynamic ": 10, 
   "test": 7, 
   "pricetype1u": 0, 
   "Price type 3": 0, 
   "Price type 2": 0  
}

my requirement is to show tooltip like this

  test price type ty dynamic : 10
  test : 7
  pricetype1u : 0
  Price type 3 : 0
  Price type 2 : 0  

please help me how can i get this required format

1
  • Do you need this format for all object tooltips? Commented Jun 29, 2020 at 6:51

1 Answer 1

2

If you have this requirement for all your object tooltips, you should consider a pipe. Copy below implementation into a pipe should be work.

If you just need this format once, you can just write a simple function to handle it:

function convertObjTooltip(obj: any) {
    const results = [];
    Object.keys(obj).reduce((sum, key) => {
        sum.push(`${key}: ${obj[key]}`);
        return sum;
    }, results);
    return results.join('\n');
}

I didn't remove the first change line, you can handle it by yourself.

Calling it in your template:

<button type="button" 
    icon="file-excel" 
    data-toggle="tooltip" 
    data-placement="top"
    title="{{convertObjTooltip(obj)}}">
Sign up to request clarification or add additional context in comments.

3 Comments

it works, thank you so much huan ... i written a console it always triggering in background , it is good practice
Yes, Actually binding property to function call is not a good idea, you can avoid this by simple assign the calculated value to variable in component, and use that value in template. This should avoid the performance issue.
as you asked me to do i created angular pipe so it will be called once only and working fine.... Thank You huan.

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.