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 badges1 Answer
Reset to default 7You 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
评论列表(0条)