0

I am writing code to share a link on twitter on clicking the twitter icon Here is my template

<a id="tw" href="https://twitter.com/intent/tweet?url=https://publish.therightdoctors.com/posts/{{data2['question_id']}}/{{encodeURIComponent(data2['question'])}}&text={{data2['question']}}&via=TheRightDoctors"
                                                        title="Share on Twitter" data-service="twitter" target="_blank"> <i class="fab fa-twitter" aria-hidden="true"></i></a>

In component.ts

import { HttpParameterCodec } from "@angular/common/http";

But it is giving me error

_co.encodeURIComponent is not a function

How to solve it?
I tried to write a function in ts. Now there is no error but on compose tweet the link is coming with space like https://publish.therightdoctors.com/posts/1/What is machine learning? but I wanted this https://publish.therightdoctors.com/posts/1/What%20is%20machine%20learning?

trdwip002
  • 9
  • 3

1 Answers1

0

Angular is trying to resolve encodeURIComponent in the context of the component, not the global javascript context. You need to have a method in your class (.ts) that does it for you, and call it from the template. Such as:

myEncodeURIComponent(text: string): string [
    return encodeURIComponent(text);
}

A similar question here see the answer confirmed there too.

Benny Halperin
  • 1,872
  • 3
  • 13
  • 18
  • I tried to write a function in ts. Now there is no error but on compose tweet the link is coming with space like https://publish.therightdoctors.com/posts/1/What is machine learning? but I wanted this https://publish.therightdoctors.com/posts/1/What%20is%20machine%20learning? – trdwip002 Dec 28 '20 at 07:26
  • @trdwip002 edit your question above and include the function you wrote. Maybe I can help – Benny Halperin Dec 28 '20 at 07:28
  • @trdwip002 please include the actual function code, the template markup that calls the function, and an example of the input. Without it it's impossible to find the problem – Benny Halperin Dec 28 '20 at 10:22
  • function is same as in answer and i have changed the function name in template – trdwip002 Dec 28 '20 at 10:29
  • @trdwip002 Apparently when enclosed in quoted attribute it does not show the encoded space %20. But for example in `{{ myEncodeURIComponent(text) }}` it does. I don't think your code should break – Benny Halperin Dec 28 '20 at 13:30