Permit the use of HTML tags within an Angular controller or view

My controller :

LandingApp.controller('LandingCtrl2', function($scope){
    $scope.articles = [
        {
            'imageSrc'   : IMG_DIR + 'spec9.jpg',
            'title'      : 'Stencils',
            'description': 'Plastic or thin metal stencils.<br/>100% Custom made'
        }
    ];
});

When I use ng repeat (....article in articles....) {{article.description}}, I receive

...  stencils.<br/>100% ....
. However, I need a way to add line breaks or other HTML tags. How can I resolve this issue?

Answer №1

To ensure that AngularJS renders a string as HTML, create a method for it.

For example:

$scope.convertToHtml = function (htmlString) {
    return $sce.trustAsHtml(htmlString);
}

In your view:

<div ng-bind-html="convertToHtml(article.content)"></div>

Remember to add $sce in your controller:

App.controller('MainCtrl', function($scope,$sce){ ...

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

Designing Object-Oriented JavaScript

Currently, I am in the process of developing a sophisticated JavaScript application. Utilizing an object-oriented design approach, I have organized my code into various files to enhance maintainability. In order to create my application, what is the best ...

Error encountered in Express.js blogging app: Issue arises when attempting to filter posts by category, resulting in a "Cast to ObjectId failed" error

Currently, I am developing a blogging application using technologies like Express, EJS, and MongoDB. In the application, I have structured posts into different categories, each stored in its own collection. However, I encountered an issue while attemptin ...

Developing a Customized Filtering Mechanism in Angular 8

I have some experience working in web development, but I am relatively new to Angular. My current project involves creating a simple filter for a table's column based on user input. However, I'm facing an issue where typing in a single letter fil ...

Tips for retrieving the posted object in angularJS

My current challenge involves creating an object with a defined name, posting it into a database, and then immediately finding its position and obtaining its id. However, I have noticed that using "get" right after "post" retrieves the data before it' ...

What is the best way to ensure the options/choices div reaches its ideal width?

Check out my StackBlitz project that I've set up In the styles.css file, you'll notice that I defined a fixed width of 650px for the option dropdown div, which is currently working fine @import '~bootstrap/dist/css/bootstrap.min.css'; ...

Fetch Timeout Issue in React Native

I am currently using fetch to retrieve data from a server. Unfortunately, I am facing issues with setting a timeout for the fetch request in case the server does not respond. This is my global fetchData class: fetchGetResp(url, token) { return fetch( ...

Vue.js app does not have the capability to display JSON data as a table

I successfully fetched JSON data in my vue app from the endpoint '/v1/api/sse?raceId=1', but I'm struggling to convert this JSON into a table format. The JSON is displaying correctly, but I'm unsure how to structure it into a table. He ...

Why does the browser keep converting my single quotation marks to double, causing issues with my JSON in the data attribute?

This task should be straightforward, but I seem to be facing a roadblock. I am trying to insert some JSON data into an input's data attribute, but the quotes in the first key are causing issues by prematurely closing the attribute. Here is the code ...

What is the process for incorporating JavaScript files into an Angular project?

I have a template that works perfectly fine on Visual Studio. However, when I try to use it on my Angular project, I encounter an issue with the JavaScript code. I have filled the app.component.html file with the corresponding HTML code and imported the ...

Am I making a mistake in my implementation of sending Socket.io messages to a designated room?

Upon a user joining, I alter their ID to a shortened random code. This change is made to utilize the id as a visible session ID on the front-end. I mention this to prevent confusion regarding non-standard socket IDs and their relation to potential issues. ...

What is the proper way to define an array of objects in TypeScript?

In search of a way to define a function that accepts an array of unspecified object types, known as "anonymous types." I want to enforce strict typing with TypeScript. The objects in the array all have properties like name, price, and description. bagTotal ...

To begin, select and upload two files for processing. Once complete, you can download the newly generated

I'm encountering difficulties attempting to upload two files to a php script and have the page download a newly merged file without redirecting to a different page. I do not wish to store any files on the server due to their potential size (2MB) as b ...

Guide on translating a date object with angular-translate

I am currently facing a challenge with localizing date objects in my Angular application. I have a list of dates displayed in my view, powered by the controller. I am using angular-translate for managing localization throughout the application, but I' ...

Remove feature in HTML file upload form

I have a basic upload form set up like this: <form method="post" action="" enctype="multipart/form-data"> <table border="0"> <tr> <td>Select XML file to upload ...

Route is not simply a component in this context. When using Routes, all component children must be either a Route or wrapped within

I am currently working on my App.js file and encountering an issue while creating paths. I have wrapped a Route element around my IsUserRedirect, but the error persists. import React, {Fragment} from 'react'; import * as ROUTES from './cons ...

Looking to halt navigation in a Chrome browser immediately after performing a click action with Selenium?

Is there a way to prevent the browser from loading immediately after a click event? Are there any workarounds or JavaScript implementations for achieving this? ...

Navigating HTML with Node.js and Express.js

I have been working on routing multiple HTML pages in my project. The index.html file loads without any issues, but when I try to load raw.html, an error message pops up: Error: Failed to lookup view "error" in views directory Below is part of my app.j ...

Information disappearing upon returning to a previous screen

Currently, I am developing a web application using AngularJS and HTML. As part of the application, I created a dashboard on the home screen (referred to as Screen A) that displays a list of clients with a summary. When a client is clicked, it navigates t ...

Securing AJAX Requests: Encrypting GET and POST Requests in JavaScipt using Node.js

Looking for a way to secure ajax post and get requests using JavaScript. The process would involve: Server generates private and public key upon request Server sends the public key to client Client encrypts data with public key Server decrypts data wit ...

Resolving the issue of "Cannot set properties of null (setting 'innerHTML') at HTMLInputElement.loader" involves identifying the root cause and implementing

Need assistance with this code. Although I believe everything is correct, it still isn't working as expected. The error mentioned in the title comes up whenever I try to use it. I've made multiple modifications to the code but it's still not ...