I was using AngularJS framework in SharePoint site and was trying to fetch data from a List and display using AngularJS view. The List had Multiple Lines of Text column with Rich Text enabled. The HTML markup looked like this:
Here description contains value from multi-line text field. However, it trims the HTML part and display only plain text.
I even tried using ng-bind-html as:
This also didn't solve the problem. This is where Angular filter came to the rescue.
Solution:
First we need to create a filter:
This would keep the format intact!
<p>{{description}}</p>
Here description contains value from multi-line text field. However, it trims the HTML part and display only plain text.
I even tried using ng-bind-html as:
<p ng-bind-html="description"></p>
This also didn't solve the problem. This is where Angular filter came to the rescue.
Solution:
First we need to create a filter:
var myApp = angular.module('myApp', ['ngSanitize']);
myApp.filter('trusted', function($sce){
return $sce.trustAsHtml;
});
Note that, with this filter we are returning the HTML as trusted. Now, this filter should be used in the HTML:<p ng-bind-html="description | trusted"></p>
This would keep the format intact!
Thanks Suresh nice post...even ng-bind-html="description" worked for me, but font colors were not getting applied in view. but when I used ng-bind-html="description | trusted" it took care of all the styles.
ReplyDeleteThanks Vikash for the comments. The formatting such as Bold and Italic works as these are added as HTML elements. This filter takes care of CSS such as font color.
ReplyDeleteWow... Thanks for your kind help.... :)
ReplyDelete@Ankit, glad it helped!
Delete