javascript - Creating new line using knockout text - Stack Overflow

I am creating SPA. I am using knockout and observable array to iterate json array. Sometimes i've

I am creating SPA. I am using knockout and observable array to iterate json array. Sometimes i've got br tag inside text, and using data-bind="text: myVar" I would like to brek line. Problem is, br tags wont work, because i can see <br /> except new line. My question is: how can I force knockout data-bind to create new line using this br tags from json data?. I was trying to use "white-space: pre-wrap", but didn't work.

I am creating SPA. I am using knockout and observable array to iterate json array. Sometimes i've got br tag inside text, and using data-bind="text: myVar" I would like to brek line. Problem is, br tags wont work, because i can see <br /> except new line. My question is: how can I force knockout data-bind to create new line using this br tags from json data?. I was trying to use "white-space: pre-wrap", but didn't work.

Share Improve this question edited Mar 27, 2015 at 10:53 Shagohad asked Mar 27, 2015 at 10:29 ShagohadShagohad 4051 gold badge9 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You just need to bind using html: instead of text: and it will process the <br />.

Run the below snippet:

var viewModel = {
  myVal: ko.observable('First Line <br />Second Line <br />Third Line')
};

ko.applyBindings(viewModel);
* {
  font-family: Arial;
  }
<script src="https://cdnjs.cloudflare./ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<h2>Text Binding:</h2>
<span data-bind="text: myVal"></span>
<h2>HTML Binding:</h2>
<span data-bind="html: myVal"></span>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744267228a4565923.html

相关推荐

  • javascript - Creating new line using knockout text - Stack Overflow

    I am creating SPA. I am using knockout and observable array to iterate json array. Sometimes i've

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信