Home » Javascript » How can properly render content that contain new line using JS?

How can properly render content that contain new line using JS?

Posted by: admin November 1, 2017 Leave a comment

Questions:

Description

I have a string look like this that I query from the database.

In the controller, I ran

dd($disclaimer);

I get

"""
q\n
a
"""

Then, I’m trying to access it the JS like this

$scope.disclaimer.text = '{!! $disclaimer or '' !!}';

I also try

$scope.disclaimer.text = JSON.parse('{!! $disclaimer or '' !!}');

I kept getting

Uncaught SyntaxError: Invalid or unexpected token


Questions

How would one go about and debug this further ?


I’m open to any suggestions at this moment.

Any hints/suggestions / helps on this be will be much appreciated!

Answers:

This seems to do the trick for me, just in case anyone ran into the same issue.

$scope.disclaimer.text = JSON.parse(JSON.stringify({!! json_encode($disclaimer) !!}));