Special characters in a knockout related data model

I have a data model with special characters in it (degree signs) .. so the property contains the following: "48 ° f". However, when the user interface updates this data, I see " ° ", and not a degree sign. I also tried " ° ", but that doesn't work either.

How to place special characters in the data model and display them in the user interface?

+4
source share
2 answers

To do this, you must use the html binding:

 <span data-bind="html: test"></span> var vm = { test: ko.observable("48 &deg; f") }; ko.applyBindings(vm); 

Here is a working fiddle: http://jsfiddle.net/svu82/

More details in the KO documentation: http://knockoutjs.com/documentation/html-binding.html

+7
source

I don't think storing data encoded in a database is a good idea.

Using ASPNET / MVC C # (with Razor) to bind information from a database to a text area value. I use C # and had to do something like this server side when installing the observable.

An example of replaced charcters, so value binding works correctly.

 model.Description = ko.observable( @html.Raw( Model.Description.Replace("\"", "\\\"") .Replace("'", "\\\'") .Replace(Environment.NewLine, "\\r\\n") ) ); 

The extension method makes binding more enjoyable.

  public static IHtmlString HtmlEscape(this String str) { if (String.IsNullOrEmpty(str)) return new HtmlString(""); var html = ((System.Web.Mvc.WebViewPage) WebPageContext.Current.Page).Html; return html.Raw( str.Replace("\"", "\\\"") .Replace("'", "\\\'") .Replace(Environment.NewLine, "\\r\\n")); } 

And binding

 Description: ko.observable("@(Model.Description.HtmlEscape())"); 

It worked for me.

0
source

Source: https://habr.com/ru/post/1481995/


All Articles