Angular 2: Dealing with Missing Image Loading

When viewing an HTML file containing the following code:

 <div>
      <img  src="New-Google-Logo.png"/>
    </div>

The image file New-Google-Logo.png is expected to load from the same folder as the HTML file. However, after running ng serve, the HTML page loads successfully with all other content except the image. It was attempted to directly link to an external image (e.g. www.google.com/images/x.png) and that worked, but the local file failed to load.

Folder Structure :

src
  -app
    -logincomponent
              - logincomponent.html
              - logincomponent.css
              - New-Google-Logo.png
              - logincomponent.ts
     -homecomponent
              - homecomponent.html
              - homecomponent.css
              - homecomponent.ts

The New-Google.png file is referenced within the logincomponent.html as shown above.

Another approach was tried :

src
  -app
    -logincomponent
              - logincomponent.html
              - logincomponent.css
              - Images
                - New-Google-Logo.png
              - logincomponent.ts

And referenced in the HTML like this:

<div>
      <img  src="./images/New-Google-Logo.png"/>
 </div>

Unfortunately, neither of these methods worked as expected.

Answer №1

If you are using angular-cli, it is recommended to store all your static assets in the assets folder. This way, you can easily reference them in your code like this:

 <div>
      <img  src="assets/images/New-Google-Logo.png"/>
 </div>

When serving the project, all static assets must be available to the client in order to display them. Angular CLI bundles the entire project into JavaScript files. There are two ways to serve static assets:

  • Place all static assets in the assets folder, which angular-cli will serve by default as specified in .angular-cli.json file
  • Alternatively, you can add the static assets folder to the assets array in the .angular-cli.json file. For example, if your images folder is within a static folder at the same level as the assets folder, you would update the JSON like this:

    "assets": [ "assets", "static/images" ]
    

Answer №2

alter the route to

<img  src="assets/images/New-Google-Logo.png"/>

then include dimensions

 <img  src="assets/images/New-Google-Logo.png" width="200" height="100">

Answer №3

When you generate a project using the CLI, you will find an assets folder. Create an image folder within it and copy all your .png images into it. Then, reference the image in your HTML using src="assets/images/AnyImage.png" to display the image. In your case, make sure you have placed your images inside the loginComponent folder. Give it a try, and it should work as expected.

Answer №4

When your CSS files contain references to images like the example below:

.topmenu a:hover {
    background:url('../images/topm_bg_right.gif') right top no-repeat;
    color:#fff;
}

You might need to include image extension wildcards in the permit all list of your WebSecurityConfig class. This is especially necessary when your application is a combination of Angular and Spring Boot, built together using ng build and deployed to the static folder.

The necessary configuration in the permit all list would look like this:

.antMatchers("/", "/*.jpg", "/*.png",
                    "/home", "/assets/**",
                    "/polyfills.js",).permitAll()

Answer №5

Effective Solution for Angular V18 in 2024

Make sure to update your asset configurations in the angular.json file

"assets": [
    {
        "glob": "**/*",
        "input": "src/assets/",
        "output": "/assets/"
    },
    {
        "glob": "**/*",
        "input": "public"
    }
],

For detailed information, check out the Angular Docs:

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

Attempting to align the text perfectly while also adding a subtle drop shadow effect

Trying to center text with a drop shadow without using the CSS property. I'm utilizing span and :before for cross-browser compatibility. Here is what it currently looks like: The HTML: <h3 title="Say hello to Stack Overflow"><span>Say ...

Unable to eliminate blue highlighting in Chrome when an element is in focus

After clicking on the search button, there seems to be a persistent blue outline that CSS is not correctly removing despite the settings I've applied. Here is the CSS code I have used: input:focus { -webkit-tap-highlight-color: rgba(0,0,0,0); outl ...

Using Laravel to connect custom fonts

After previously discussing and experimenting with linking custom font files in Laravel, I encountered an issue where the fonts were not displaying correctly on my website. Due to Laravel's router, directly linking to font files in CSS was posing a ch ...

Issues with Gulp Autoprefixer Functionality

I'm struggling to make Autoprefixer work with Gulp. Despite using opacity, gradients, and transforms in my CSS, I can't see any vendor prefixes being added. However, everything else seems to be functioning correctly. Below is my gulp file: var ...

The date format in AngularJS is not being displayed correctly according to the request

My goal is to retrieve the date in the format of dd/MM/yyyy and pass it along to my URI. However, I am encountering an issue where the date is not being passed in the requested format. Below is the snippet of my HTML code: <script type="text/ng-templat ...

Execute consecutive Angular2 functions in a sequential manner, one following the next

In my Angular2 project, I have a service that fetches data for dropdown menus on a form. However, when I call this service multiple times with different parameters in the form component initialization, only the last call seems to work, overriding the previ ...

Creating a visually dynamic stack of images using javascript, jquery, and HTML

I'm interested in creating a unique image viewer using javascript/jQuery/HTML that combines elements of a book page flip and iTunes coverflow, optimized for mobile device browsers. I've been searching for tutorials to help kickstart this project, ...

Arrange DIV elements sequentially with HTML, JQuery, and CSS

Live Demo: Live Demo HTML: <div class="target"> <img src="bg-clock.png" alt="jQuery" /> </div> <div class="target2"> <img src="bg-clock.png" alt="jQuery" /> </div> CSS: .target, .target2 { ...

Tips for including numerous hyperlinks in a single row for a website's navigation menu

I need assistance in creating a navigation bar for my website that includes multiple headers with links. I am not sure if the issue lies within my CSS, HTML, or both. Can someone please help me troubleshoot? index.html <!DOCTYPE html> <html> ...

Leverage the power of jQuery to manipulate video tags

Here's the challenge: I need to utilize a jQuery function to extract a URL from a link's REL attribute and then transfer it to a video element. The extraction process and transmission seem to be working smoothly. However, my struggle lies in act ...

How to perfectly position various height <a> elements in a WordPress mega menu

I'm currently working on a WordPress mega menu with four columns, each with a heading or top menu item. The headings should have a gradient border at the bottom and differ in length. However, I'm facing an issue where some headings span two lines ...

Utilizing Angular: Integrating the Http response output into a map

I have a situation where I am making multiple HTTP calls simultaneously from my Angular application. The goal is to store the responses of these calls in a Map. data: Map<number, any> = new map<number,any>(); --------------------------------- ...

The React Navbar fails to appear when using React JS

I am currently in the process of creating a single-page website. I have incorporated react-bootstrap for the navigation bar, but I am facing an issue where it is not rendering on the page. Despite not encountering any errors, the page remains blank. Below ...

When the pathway is void, it becomes impossible to redirect to the designated component

I recently developed a code that is supposed to redirect to a specific component when the application starts. const routes: Routes = [ {path: 'home', component: HomeComponent}, {path: 'content', loadChildren: 'app/componen ...

Upgrading from Angular 6 to Angular 7

After upgrading my Angular 4 app to Angular 6, I'm now looking to make the jump to Angular 7. According to a recent article, running the command below should only take around 10 minutes for the upgrade process: ng update @angular/cli @angular/core F ...

Formatting PDF exports in Angular using jspdf

I am attempting to export a table, but I am facing an issue with the formatting. The table I want to export looks similar to this: https://i.sstatic.net/WdBzv.png Below is the function I am using: public downloadAsPDF() { const doc = new jsPDF( ...

Using jQuery to add a class to an image element when the source attribute contains a specific

I have a bunch of text that looks like this <div id="justThisDIV"><p>Some paragraph <img src="http://mydomain.com/image.jpg"/> </p> <p><img src="http://mydomain.com/wow.png"/></p> <p><img src="http://notm ...

Using JQuery, identify cells located in the first column of a table excluding those in the header section

In the past, I had code that looked like this: $(elem).parents('li').find(...) I used this code when elem was an item in a list, making it easy to reference all items in the list. But now, I've made some changes and decided to use a table ...

Building a user interface in Angular2 that consists of multiple components and utilizes

What is the recommended structure for an Angular2 (beta3) application with routing when incorporating a parent/child multi-component setup? When dealing with individual tables, I have set up the following structure: https://i.stack.imgur.com/BYqGU.jpg I ...

Incorporating AngularJS to show sections of a webpage in real-time

, I am working on an angular js application in which I'm using rest services to retrieve http response data. My goal is to display different parts of the webpage conditionally based on the response data. For example, if the data in angular indicates " ...