Abhinandan Khilari
Abhinandan Khilari

Reputation: 1077

Angular: Custom pipe event handling

This was asked in an interview.

Requirement is to create a custom pipe that would return a shortened text suffixed by a 'Read more' link for a text if it has more than a certain number of characters, lets say 10 characters.

If clicked on the 'Read more' link, the entire original text should be displayed suffixed by a 'Read less' link. If clicked on the 'Read less' link, again the shortened text should be displayed suffixed by a 'Read more' link. Thus, 'Read more' and 'Read less' links and short and original text should toggle on click of the links.

This entire functionality is to be implemented by a pipe only. Is event handling possible in a pipe? How we can handle events for the clicks on the links in the pipe?

Upvotes: 0

Views: 541

Answers (1)

Suhas Parameshwara
Suhas Parameshwara

Reputation: 1454

I think the interviewer doesn't know the actual use of a pipe. Basically a pipe shouldn't handle like these kinds of complex operations. A pipe should handle some basic operations like

  • Changing date time to locale
  • Trim the string
  • sorting the numbers based upon conditions etc.,

The best choice would be using a Shared Component. He can use this to communicate events between the components.

Upvotes: 1

Related Questions