0

I have created a factory to retrieve json data from my backend. Parts of the json data is heavily text-based and needs to convert line breaks into coupled <p></p> tags.

I tried to use this function to process the text in my view file.

function TextProcess(text) {
    var p_text = text.replace("/[\r\n]+/", "</p><p>")+"</p>";
    var finish_text = p_text.replace("/(?<=\s)\x20|\x20(?=\s)/", "&nbsp;");
    return(finish_text);
}

It is possible that this function has its own problem...(I'm very new to javascript)

Then in view, I tried:

<p>{{TextProcess(article.body)}}</p>

This line gets me nowhere.

Is what I am doing the right way to process text in AngularJS? I have no idea how to process Json data using AngularJS without the help of ng-repeat.

Please tell me what to do.

1 Answer 1

1

You can do this is using a custom filter to apply formatting to the text.

You will end up writing this in your template:

<span ng-bind-html-unsafe="text | customFilter"></span>

Here is the custom filter code (i named it 'fixup')

.filter('fixup', function () {

return function (input) {
    var txt = input.replace("/[\r\n]+/", "</p><p>") + "</p>";
    var out = txt.replace("/(?<=\s)\x20|\x20(?=\s)/", "&nbsp;");

  return out;
}

I created a fiddle, but the substitution isn't working so well, but it should give a solid starting point: http://jsfiddle.net/aNp8M/2/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!!!! It worked. I changed my RegExp of course..this is how I wrote on my template. I'm not sure if this is the right way to do: <p ng-bind-html-unsafe="article.body | textProcess">{{article.body}}</p>
glad to help. please mark the question as the answer and/or vote it up.

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.