0

I am using AngularJS. I have a scope variable $scope.TextToUser which is printed in html to be shown to user on website. In my controller, the code is simple and looks like this;

$scope.TextToUser='UserText'

On the corresponding html page, the code is just {{TextToUser}}.

I tried to turn the text to red by using the following code in the controller;

$scope.TextToUser = '<font size="3" color="red"> 'UserText'</font>'

Unfortunately, on the html page, the entire string was printed and not UserText in red. How can I turn the text red? Simple, straight-forward methods will be preferred.

1
  • What happened to all the earlier answers? Got deleted? They looked promising. Commented Nov 20, 2014 at 2:55

1 Answer 1

1

Well, following is more proper:

app = angular.module('<name of your app>', ['ngSanitize']);

app.controller('<controller name>', function($scope, $interpolate){
    $scope.Text = 'Test to User In Red';
    $scope.TextToUser = $interpolate('<font size="3" color="red">{{Text}}</font>')($scope);
});

Now bind html:

<span ng-bind-html="TextToUser"></span>

You need to include sanitize script. https://code.angularjs.org/1.3.2/angular-sanitize.js

Note:- I tried using $interpolate but it gives error about unsafe stuff without ngSanitize.

The working plunk is here.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Got worried what happened to your answer. It suddenly disappeared just now. Will try it out. Many thanks again.
@user768421 was putting up plunk with proper way of doing it.
Wow. So helpful of you. THanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.