Images are not appearing when using ng-repeat

const createFullName = (first, last) => {
    return `${last}, ${first}`;
};

let partyImage = "";
if(party=="R") {
    partyImage = "<img src='/images/r.png'>";
} else {
    partyImage = "<img src='/images/d.png'>";
}

let chamberImage = "";
if(chamber == "senate") {
    chamberImage = "<img src='/images/s.svg'> Senate";
} else {
    chamberImage = "<img src='/images/h.png'> House";
}

$scope.users.push({
    party_name: partyImage,
    fullname: createFullName(first_name, last_name),
    chamber_name: chamberImage,
    district_name: district,
    state_name: state
});

The HTML part

<tr ng-repeat="user in users">
    <td>{{user.party_name}}</td>
    <td>{{user.fullname}}</td>
    <td>{{user.chamber_name}}</td>
    <td>{{user.district_name}}</td>
    <td>{{user.state_name}}</td>
</tr>

When attempting to display the images, they are shown as tags instead of displaying the actual images. The other variables in the push function are Strings and are displayed correctly. Can someone assist me with this issue?

Answer №1

Retrieve only the src from the image and use it as a string

var name = last_name + ',' + first_name;
if(party=="R")
    party="/images/r.png";
else
    party="/images/d.png";

if(chamber == "senate")
    chamber="/images/s.svg";
else
    chamber="/images/h.png";

$scope.users.push({
    party_name:party,
    fullname:name,
    chamber_name:chamber,
    district_name:district,
    state_name:state});

Then in the html incorporate an <img> element with the src attribute tied to binding

<tr ng-repeat="user in users">
    <td> <img src="{{user.party_name}}" /> </td>
    <td>{{user.fullname}}</td>
    <td> <img src="{{user.chamber_name}}" /> </td>
    <td>{{user.district_name}}</td>
    <td>{{user.state_name}}</td>
</tr>

PLUNKER

Answer №2

Consider implementing the following code snippet.

var name = last_name + ',' + first_name
if(party=="R")
    party='/images/r.png';
else
    party='/images/d.png';

if(chamber == "senate")
    chamber='/images/s.svg';
else
    chamber='/images/h.png';

$scope.users.push({
    party_name:party,
    fullname:name,
    chamber_name:chamber,
    district_name:district,
    state_name:state});
enter code here

Then in your HTML code, add the tag

    <tr ng-repeat="user in users">
        <td>{{user.party_name}}</td>
        <td>{{user.fullname}}</td>
        <td><img src="{{user.chamber_name}}"/> </td>
        <td><img src="{{user.district_name}}"/></td>
        <td>{{user.state_name}}</td>
    </tr>

I hope this solution proves helpful to you.

Answer №3

Keep in mind to utilize ng-src for image sources.

<td><img ng-src="{{user.profile_pic}}"/></td>

Angular documentation

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

Retrieve information using AJAX via POST method

I find myself in a bit of a pickle at the moment. I've been researching for hours, and I still can't seem to figure out this seemingly basic issue. It would be greatly appreciated if someone could offer me some quick advice. So here's my dil ...

Incorporating an HTML file into a DIV container while also displaying menu links

I am facing a major challenge with what I recognize as a relatively simple issue for experts. As someone who is still learning, I am struggling to create menu links that load an HTML file into a specific DIV. Despite my efforts, the content keeps loading i ...

What is the process for making an Ajax request in Rails?

I'm attempting to send a variable through jQuery using the POST method, saving it in a controller, and then utilizing that same variable in Rails HTML to query a table. It seems like the variable isn't being passed to the controller. jQuery: v ...

Unable to execute a JavaScript function when triggered from an HTML form

This is the code for a text conversion tool in HTML: <html> <head> <title> Text Conversion Tool </title> <script type="text/javascript"> function testResults(form) { var str = form.stringn.value; var strArray = str.split(" ...

Waiting for Angular's For loop to complete

Recently, I encountered a situation where I needed to format the parameters and submit them to an API using some code. The code involved iterating through performance criteria, performance indicators, and target details to create new objects and push them ...

Loop through an array of div IDs and update their CSS styles individually

How can I iterate through an array of Div IDs and change their CSS (background color) one after the other instead of all at once? I have tried looping through the array, but the CSS is applied simultaneously to all the divs. Any tips on how to delay the ...

Guide to importing a JSON file into Vue.js and HTML

I'm a beginner in Vue and not very familiar with HTML I'm attempting to import data from a JSON file into my interface to display it for the user. Below is the structure of the JSON: [ { "Title": "SOFT-STARTER", "Cod&q ...

A Step-By-Step Guide on Displaying Two Forms with Two Buttons in HTML

My goal is to develop a functionality on a webpage where users can create keys in a database. The user will need to select between two buttons, each button representing a different form to be displayed. The challenge I am facing is being able to utilize o ...

Javascript - combining properties using "concatenation"

I'm currently pulling data from JSON for a three.js scene. My goal now is to transform data such as "position.x : 1" into object[ position ] [ x ] = 1. The syntax object [ key ] = value isn't effective in this case. This would result in object[ ...

AngularJS directive for Ionic Leaflet - Utilizing Service to switch tileLayer from side menu

I am currently experimenting with developing an ionic app for the leaflet-angularjs-directive. Unfortunately, there are not many demos available for me to test out. The specific demo I am using is called ionic-leafletjs-map-demo by calendee on GitHub whic ...

How can I ensure that all row checkboxes are automatically marked as checked upon loading the Material Table in ReactJS?

I'm currently working on a project using React Material Table and I need to have the selection option pre-checked by default. Is there a way to accomplish this? function BasicSelection() { return ( <MaterialTable title="Basic Selec ...

Challenges with implementing Node Polyfills in webpack configuration file

I recently started delving into web development and currently tackling a React project that involves connecting to an AWS database. After installing MySql using npm install mysql, I encountered an error message: Module not found: Error: Can't resolve ...

Switch up the link color when it pops up

Is there a method to change the link color to gray without encountering glitches like on my site, where it mistakenly shows "Quick Nav."? Click here to view the page with the glitch I only want this particular link to be bold and not any other links. He ...

Using Alpine JS to rotate through images at regular intervals using the window.setInterval() method

Attempting to tackle a simple task using Alpine JS and the standard JS function setInterval. The goal is to create an image selector where images switch every second (1000ms). Here's what I have so far: <div x-data="imgFunc()"> ...

jQuery is great at adding a class, but struggles to remove it

When you click on an anchor with the class .extra, it adds the "extra-active" class and removes the "extra" class. However, when you click on an anchor with the extra-active class, it does not remove the extra-active class and replace it with extra. Here ...

Tips for efficiently loading data into a vuex module only when it is required and addressing issues with async/await functionality

Is there a method to load all the data for a Vuex store once and only load it when necessary? I believe there is, but I am having trouble implementing it. I'm not sure if it's due to my misunderstanding of Vuex or Async/Await in Javascript promi ...

Incorrect measurement of text size

My attempt at creating a basic font size changer was working perfectly until I integrated it with the bootstrap framework. Strangely, when I try to increase the font size, it actually decreases instead. var baseFontSize; (function () { "use strict"; ...

What is the process for combining two services into a single, unified service?

In my Angular 8 application, I have a service structured like this: export class ProfileUserService { user$ = this.authService.loginStatus().pipe(take(1)); constructor(private profileService: ProfileService, private authService: AuthService) {} ge ...

"Customize your text alignment with TinyMCE's align

I'm in the process of updating from an outdated version of TinyMCE to the most recent release. In the old TinyMCE, if you inserted an image and aligned it to the left, the HTML generated looked like this: < img src="testing.jpg" align="left" > ...

React Router v6 is throwing an error stating that within the <Routes> component, all children must be either a <Route> or <React.Fragment>

While the code snippet below may have worked perfectly fine in React Router v5, it's causing an error in React Router v6. Error: [Player] is not a <Route> component. All component children of <Routes> must be a <Route> or <React ...