Knockout.js carraige return in paragraph text

后端 未结 2 1001
栀梦
栀梦 2021-02-05 03:37

Using knockout.js, how do I include a carriage return in the text that is bound to the text attribute of a paragraph

element.

In my ViewModel I g

相关标签:
2条回答
  • 2021-02-05 03:49

    You can use the html binding.

    JS:

    function AppViewModel() {
        this.firstName = "Bert<br\>Test";
        this.lastName = "Bertington";
    }
    
    // Activates knockout.js
    ko.applyBindings(new AppViewModel());
    

    View :

    <p>First name: <strong data-bind="html: firstName">todo</strong></p>
    <p>Last name: <strong>todo</strong></p>
    

    See fiddle

    0 讨论(0)
  • 2021-02-05 03:55

    You need to set a css property in your element. white-space: pre-wrap

    <p style="white-space: pre-wrap">First name: <strong data-bind="text: firstName">todo</strong></p>
    <p>Last name: <strong>todo</strong></p>
    
    function AppViewModel() {
        this.firstName = "Bert" + " \n " + "Test";
        this.lastName = "Bertington";
    }
    
    // Activates knockout.js
    ko.applyBindings(new AppViewModel());
    

    Then the code works. with \n

    0 讨论(0)
提交回复
热议问题