How to get line break within string interpolation in Angularjs

前端 未结 5 1828
萌比男神i
萌比男神i 2021-01-05 10:15

So I\'m trying to do something very simple and I\'m stuck. I have a String variable and within that variable I Wanna set line break so certain part of the text

5条回答
  •  抹茶落季
    2021-01-05 10:56

    Here are two demonstrably working versions...

    White Space

    Solution One... if you want newlines to be respected in HTML... (works with the back-tick strings, or with 'My \ntitle'...

    document.getElementById('example').innerHTML = `My
    title`;
    h1 {
      white-space: pre;
    }

    Angular Version:

    {{title}}

    HTML Break

    Solution two... you want to use HTML...

    document.getElementById('example').innerHTML = 'My
    title';

    Use ng-bind-html if you want to allow HTML during binding.

提交回复
热议问题