0

If I have a simple user status dropdown, such as:

<a class="dropdown-toggle">
   {{userStatus}}
</a>
<ul class="dropdown-menu" role="menu">
    <li data-ng-click="SwitchStatus('Online')"><i class="fa fa-circle text-green2 pr5"></i> Online</a></li>
    <li data-ng-click="SwitchStatus('Busy')"><i class="fa fa-circle text-red2 pr5"></i> Busy</li>
    <li data-ng-click="SwitchStatus('Away')"><i class="fa fa-circle text-orange2 pr5"></i> Away</li>
</ul>

And the controller has this:

var userStatuses = {
    online: '<i class="fa fa-circle text-green2 pr5"></i>',
    busy: "<i class='fa fa-circle text-red2 pr5'></i>",
    away: "<i class='fa fa-circle text-orange2 pr5'></i>"
};

$scope.userStatus = userStatuses.online; // default online

Why does it inject the HTML into the page like this:

<a class="dropdown-toggle">
    "<i class='fa fa-circle text-green2 pr5'></i>"
</a>

What can I do so that it injects it as an HTML element and not a string?

Also, it's obvious what I'm trying to achieve, and I have just started with angular, so feel free to point me in another direction. I want to eventually incorporate advanced logic when the different statuses are selected.

Thank you.

3 Answers 3

2

In that specific case I would use ng-class instead of pasting HTML in the template.

AngularJS isn't jquery.

see:

<a class="dropdown-toggle">
  <i class='fa fa-circle pr5' ng-class="{'text-green2': userStatuses.online, 'text-red2': userStatuses.busy, 'text-orange2': userStatuses.away}"></i>
</a>

and the JavaScript

var userStatuses = {
  online: true,
  busy: false,
  away: false
};
scope.userStatuses = userStatuses;

Hope this whas helpfull. Good Luck!

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

6 Comments

Thank you. I will use this. I'm still curious however, to understand why it was injected as HTML?
Speaking of your jQuery comment, this has been the biggest struggle for me so far is doing things the "Angular Way"... I've only been at this a few days.
It's a js string, not an HTML element, if you could paste a string as HTML just like that, the model's field couldn't have characters like "<" or ">", or the view would all be messed up.
As mentioned in my answer, Angular won't render HTML in a ng-bind or similar directives to prevent XSS. If you want to render HTML as actual HTML, you should be using the ng-bind-html directive. That said, this answer solves this problem better than mine.
@SebastianGabrielVinci Ah, you beat me to it.
|
0

This is a use-case for ng-bind-html. Angular doesn't like inserting HTML unless explicitly told to do so, so as to prevent XSS vulnerabilities.

Your dropdown should look like:

<a class="dropdown-toggle" ng-bind-html="userStatus"></a>

Comments

0

Perhaps I think a better way would be to keep your html out of your controller and try using ng-class to change the class of your icon based on the userStatus object:

<a class="dropdown-toggle">
   <i class="fa fa-circle pr5"
      ng-class="{{ 'text-green2' : userStatus.online,
                   'text-red2' : userStatus.away,
                   'text-orange2': userStatus.busy }}">
   </i>
</a>

In the controller, skip the var declaration you're using and do this

$scope.userStatus = userStatus;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.