1

I'm receiving an array of strings from a REST api call, and the strings have some html tags in them, for example:

Bard's opponents can also travel through his Magical Journey doorways. You can follow him, if you think it's safe.</li>

You can crush Bard's healing shrines just by walking over them. Don't let his allies take them without a fight.

Bard's ultimate, Tempered Fate, affects allies, enemies, monsters, and turrets alike. Sometimes it can be to your advantage to jump into it!</li>

Each line is a string, and two of them have an </li> tag at the end.

I tried writing a function that receives such an array, and returns a corrected array. Problem is, when I use it, the console in my website shows some weird errors with the strings from the array, and I've realized my function was the cause.

That is the function:

modal.removeBracketsFromArray = function (array) {
    if (array == undefined)
        return array;

    function removeBracketsText(text) {
        return text.replace(/<[^>]*>/g, '')
    };

    var newArray = [];
    for (var i = 0; i < array.length; i++) {
        newArray.push(removeBracketsText(array[i]));
    }

    return newArray;
 };

It seems to do the work, but it somewhy messes up when using it in an ng-repeat attribute.

This is an use example:

<champion-ally-enemy-tips allytips="modalCtrl.removeBracketsFromArray(modalCtrl.champ.allytips)"
                          enemytips="modalCtrl.removeBracketsFromArray(modalCtrl.champ.enemytips)">
</champion-ally-enemy-tips>

which then moves to:

<ul>
    <li ng-repeat="enemytip in enemytips"><h6>{{enemytip}}</h6></li>
</ul>

When I remove the method call (like so), it doesn't show an error, but the tags remain:

<champion-ally-enemy-tips allytips="modalCtrl.champ.allytips"
                          enemytips="modalCtrl.champ.enemytips">
</champion-ally-enemy-tips>

Is my function doing something weird without realizing it? thanks for helping

This is a pastebin of the errors I receive: LINK

4
  • 1
    Out of curiosity, you're doing a website about league of legends :P ? Commented May 12, 2016 at 19:06
  • Definitly :P Wanted to learn angular, and have seen riot api website for several times, thought it'd be nicer to show teemo over some weather api stuff :P Commented May 12, 2016 at 19:08
  • Do you by chance have angular unminified version? Maybe that error will be clearer. Also do you have the champion-ally-enemy-tips component we can see? Commented May 12, 2016 at 19:19
  • @Lex already answered it. Commented May 12, 2016 at 19:31

1 Answer 1

2

Instead of messing with the array, how about creating a custom filter to strip out the HTML on display?

.filter('removeHTML', function() {
    return function(input) {
        return input.replace(/<[^>]*>/g, '');
    }
})

Then change your display inside the ng-repeat to:

<h6>{{enemytip | removeHTML}}</h6>
Sign up to request clarification or add additional context in comments.

3 Comments

Nice idea! didn't think if it. Let me check it out and return an answer
Beautiful, works and there's no error. Thanks a lot! :)
Glad I could help. :)

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.