Enhancing Angular select functionality by incorporating HTML entities using ng-option

My query is somewhat linked to this specific response, although with a slight variation.

What I am aiming to accomplish is the parsing of HTML entities from a string passed onto a select using ng-options. Consider the following dataset:

$scope.myOptions = [{
   text: '10.00 €',
   val: 10
},{
   text: '25.00 €',
   val: 25
},{
   text: '50.00 €',
   val: 50
}];

And this template:

<select class="form-control" ng-model="desiredAmount" 
        ng-options="opt.val as opt.text for opt in myOptions">
</select>

I wish for the &euro; entity to be displayed as €, rather than its actual format (I understand why it's not working, as Angular interprets the data as plain text). Is there a solution to this issue?

Here is a jsbin that I created to illustrate my predicament.

PS - I am aware that I can achieve this using

<option ng-repeat="opt in myOptions">{{opt.text}} &euro;</option>

However, this creates an additional problem with an empty initial option appearing, which is why I prefer sticking to the ng-option method. Regardless, I would like to know if it is feasible to have a string parsed when utilizing ng-option.

Answer №1

To display the euro sign using $sce, follow these steps:

Use the following code in your HTML file:

  <select>
      <option ng-repeat="opt in myOptions" ng-bind-html="htmlAdText(opt.text)">{{opt.text}}</option>
  </select>

Add this JavaScript code to your controller:

  $scope.desiredAmount = $scope.myOptions[0].text;

  $scope.htmlAdText = function(text){
    return $sce.trustAsHtml(text);
  }

Remember to include $sce as a dependency in your controller.

http://jsbin.com/tijevebiya/2/edit?html,js,output

Answer №2

If you are dealing with the euro issue, there is a simple solution using a filter to show the value properly. This filter changes the currency and formats all values in this way: €0.00. Additionally, you have the option to customize the currency symbol.

To implement this, use the following code:

{{opt.val | currency:"€"}}

For more details, refer to this link:

http://docs.angularjs.org/api/ng.filter:currency

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am curious about the significance of the "=>" symbol within the Ionic framework

I utilized the documentation provided on the Ionic website to incorporate Firebase into my mobile application. this.firebase.getToken() .then(token => console.log(`The token is ${token}`)) // store the token server-side and utilize it for sending not ...

The component next/image is experiencing issues when used in conjunction with CSS

I struggled to create a border around an image because the custom CSS I wrote was being overridden by the Image component's CSS. Despite trying to leverage Tailwind and Bootstrap to solve the problem, my efforts were unsuccessful. Now, I am at a loss ...

(basic) Issue with Jquery ajax request not receiving data

The alert box is not displaying anything and is not returning any data from the specified URL, even though it should show the Google page! Any suggestions? I am using the POST method because I need to send querystring data as well. $.ajax({ ...

Tips for making an input form that triggers an alert popup

After creating an HTML form with text input, utilizing Javascript for validation as shown below: I am trying to trigger an alert box thanking the user for entering data when the submit button is clicked. I have faced challenges in implementing this witho ...

Create a new array by applying a filtering function to the provided data in JavaScript using React

I am a beginner in the world of React. I have an object containing an array (Map) as follows: "POSSIBLE_UPDATE_OPTIONS": { "Process": ["confirm"], "Confirmed": [ "Process", ...

Having issues with TableExport.js exporting Bootstrap HTML tables

I've been trying to use the TableExport.js plugin found at to add Bootstrap HTML table export functionality to my website. I meticulously followed all the steps to include jquery FileSaver, tableExport javascripts, and css. <!-- jQuery --> &l ...

Which is the superior choice: Classic Ajax or jQuery Ajax?

I have a question about using AJAX. I'm stuck between choosing traditional AJAX and jQuery AJAX. When I use jQuery AJAX, sometimes it behaves strangely in Internet Explorer. This is surprising to me because jQuery is supposed to be a cross-browser lib ...

Activate the action using the onclick interaction

window.addEventListener(mousewheelEvent, _.throttle(parallaxScroll, 60), false); My current setup involves an event listener that responds to a mousewheelEvent by executing a function. However, when attempting to directly trigger this function on a separa ...

Failure of Laravel controller to process Ajax request

Currently, I am utilizing Laravel 4.2 and have an HTML onchange function as depicted in the image. However, I am encountering an error similar to the one shown in the second image. The issue lies in the fact that this AJAX request works perfectly fine on ...

When using Angular.js, I am encountering an unfamiliar service provider error while attempting to implement the animation module. Could this issue be related to how I am declaring modules without using "var ="?

The tutorial on Angular seems inconsistent when defining modules. On one hand, it shows the module defined like this: angular.module('appname', ['phonecatFilters','phonecatAnimations']). config(['$routeProvider', ...

Tips for distinguishing the beginning and ending points of wrapped text in the Firefox browser

Within my work, I am utilizing a contentEditable span where I aim to position an element with position: absolute on the same line as the cursor. However, issues arise when text wrapping occurs - causing abnormal behavior at the start and end of wrapped lin ...

Deactivating the class from a button

On my website, I have 3 buttons that represent different product categories. The initial state of the page should load with the "All Products" button having an active class. However, when clicked, this active class should be removed from the "All Products" ...

Error in identifying child element using class name

I need help accessing the element with the class "hs-input" within this structure: <div class = "hbspt-form".......> <form ....class="hs-form stacked"> <div class = "hs_firstname field hs-form-field"...> <div class = ...

What could be the reason behind my Vue custom directives not functioning as expected?

I'm brand new to Vue and very inexperienced with custom directives. I'm attempting something basic, but it doesn't seem to be working correctly. Can someone please help me figure out what's wrong? Thank you in advance! I have created tw ...

Invoke OnSelectedIndexChanged from GridView prior to any other function execution

In my scenario, there are two GridViews available. The first GridView allows the user to select a row, based on which a corresponding list will be displayed according to the selected GridView ID. First Grid: https://i.stack.imgur.com/d0OKq.png Second Gri ...

Transferring a zipped file between a Node.js server and a Node.js client

I am facing an issue with sending a zip file from a node.js server to a node.js client. The problem is that when I try to save the zip file, it becomes corrupted and cannot be opened. To create and send the zip file to the client, I am using the adm-zip l ...

Tips for transferring a jQuery array to PHP

I am encountering an issue when trying to send a jQuery array to PHP. Initially, I have one form in HTML and upon clicking 'add', I end up with two forms. Afterwards, I input data into the form which is then stored in a jQuery array. However, I a ...

In order to ensure proper alignment, adjust the width of the select element to match the width of the

Is there a way for the select element to adjust its width based on the length of the selected option? I want the default option value's width to be shorter, but once another option is selected, I'd like the select element to resize itself so that ...

Receiving error message "51 F02 FacebookConnectPlugin sClass not found" while attempting Facebook login with Ionic and Cordova

I have been working on integrating Facebook login into my application. I followed the steps outlined in this Ionic blog: "". However, I am facing an issue where the login process is failing at $cordovaFacebook.login and not proceeding further. $scope.fb ...

Prevent unexpected page breaks in <tr> elements on Firefox (Could JavaScript be the solution?)

Wondering if there are any ways, possibly through JavaScript or CSS, to avoid having page breaks within <tr> elements specifically in Firefox. Given that FF does not yet fully support page-break-inside, I assume this might need to be addressed using ...