Angular 4 img src is not found - html

I'm having a problem with sourcing an image with angular 4. It keeps telling me that the image is not found.
Folder structure:
app_folder/
app_component/
- my_componenet
- image_folder/
- myimage
I am using the image in mycomponent. If in mycomponent, I insert it directly with:
<img src="img/myimage.png"
This works fine, but I checked the html and it creates a hash. But my images have to be dynamically added to the html because they are part of a list. So, if I use:
<img src="{{imagePath}}">
which is basically img/myimage.png, it doesn't work. If I use:
<img [src]="imagePath">
It says NOT FOUND (htttps://localhost/img/myimage.png). Can you help me?

AngularJS
<img ng-src="{{imagePath}}">
Angular
<img [src]="imagePath">

Copy your images into the assets folder. Angular can only access static files like images and config files from assets folder.
Try like this: <img src="assets/img/myimage.png">

Create image folder under the assets folder
<img src="assets/images/logo_poster.png">

Angular 4 to 8
Either works
<img [src]="imageSrc" [alt]="imageAlt" />
<img src="{{imageSrc}}" alt="{{imageAlt}}" />
and the Component would be
export class sample Component implements OnInit {
imageSrc = 'assets/images/iphone.png'
imageAlt = 'iPhone'
Tree structure:
-> src
-> app
-> assets
-> images
'iphone.png'

in your component assign a variable like,
export class AppComponent {
netImage:any = "../assets/network.jpg";
title = 'app';
}
use this netImage in your src to get the image, as given below,
<figure class="figure">
<img [src]="netImage" class="figure-img img-fluid rounded" alt="A generic square placeholder image with rounded corners in a figure.">
<figcaption class="figure-caption">A caption for the above image.</figcaption>
</figure>

#Annk you can make a variable in the __component.ts file
myImage : string = "http://example.com/path/image.png";
and inside the __.component.html file you can use one of those 3 methods :
1 .
<div> <img src="{{myImage}}"> </div>
2 .
<div> <img [src]="myImage"/> </div>
3 .
<div> <img bind-src="myImage"/> </div>

Well, the problem was that, somehow, the path was not recognized when was inserted in src by a variable. I had to create a variable like this:
path:any = require("../../img/myImage.png");
and then I can use it in src. Thanks everyone!

<img src="images/no-record-found.png" width="50%" height="50%"/>
Your images folder and your index.html should be in same directory(follow following dir structure).
it will even work after build
Directory Structure
-src
|-images
|-index.html
|-app

Angular can only access static files like images and config files from assets folder. Nice way to download string path of remote stored image and load it to template.
public concateInnerHTML(path: string): string {
if(path){
let front = "<img class='d-block' src='";
let back = "' alt='slide'>";
let result = front + path + back;
return result;
}
return null;
}
In a Component imlement DoCheck interface and past in it formula for database. Data base query is only a sample.
ngDoCheck(): void {
this.concatedPathName = this.concateInnerHTML(database.query('src'));
}
And in html tamplate <div [innerHtml]="concatedPathName"></div>

You have to mention the width for the image as default
<img width="300" src="assets/company_logo.png">
its working for me based on all other alternate way.

An important observation on how Angular 2, 2+ attribute bindings work.
The issue with [src]="imagePath" not working while the following do:
<img src="img/myimage.png">
<img src={{imagePath}}>
Is due your binding declaration, [src]="imagePath" is directly binded to Component's this.imagePath or if it's part of an ngFor loop, then *each.imagePath.
However, on the other two working options, you're either binding a string on HTML or allowing HTML to be binded to a variable that's yet to be defined.
HTML will not throw any error if you bind <img src=garbage*Th_i$.ngs>, however Angular will.
My recommendation is to use an inline-if in case the variable might not be defined, such as <img [src]="!!imagePath ? imagePath : 'urlString'">, which can be though of as node.src = imagePath ? imagePath : 'something'.
Avoid binding to possible missing variables or make good use of *ngIf in that element.

Check in your.angular-cli.json under app -> assets:[] if you have included assets folder.
Or perhaps something here: https://github.com/angular/angular-cli/issues/2231, can help.

You must use this code in angular to add the image path. if your images are under assets folder then.
<img src="../assets/images/logo.png" id="banner-logo" alt="Landing Page"/>
if not under the assets folder then you can use this code.
<img src="../images/logo.png" id="banner-logo" alt="Landing Page"/>

Angular-cli includes the assets folder in the build options by default. I got this issue when the name of my images had spaces or dashes.
For example :
'my-image-name.png' should be 'myImageName.png'
'my image name.png' should be 'myImageName.png'
If you put the image in the assets/img folder, then this line of code should work in your templates :
<img [alt]="My image name" src="./assets/img/myImageName.png'">
If the issue persist just check if your Angular-cli config file and be sure that your assets folder is added in the build options.

Add in angular.json
"assets": [
"src/favicon.ico",
"src/assets"
],
And then use it like this: <img src={{imgPath}} alt="img"></div>
And in ts: storyPath = 'assets/images/myImg.png';

Try This:
<img class="img-responsive" src="assets/img/google-body-ads.png">

This was my problem, I added src/app/types/types.d.ts with content:
declare module '*.png' {
const value: any
export default value
}
declare module '*.jpg' {
const value: any
export default value
}
declare module '*.svg' {
const value: any
export default value
}

Related

I can't show an image on a page of a vuetify project

I have the folders sorted like this
As you can see all the images are stored in assets folder.
In my html page I have this tag:
<img :src="imageLink">
where imageLink is a string saved as so:
imageLink = '/src/assets/Zucchina.jpg'
but I have an error cause the source is not found.
I've inspected the sources on the web page and the assets folder is not loaded.
What am I missing?
Try one of the following
Webpack
And the image to something like this
// imageLink = "#/assets/Zucchina.jpg"
<img :src="require(`${imageLink}`)" />
or
// imageName = "Zucchina.jpg"
<img :src="require(`#/assets/${imageName}`)" />
Vitejs
// imageName = "Zucchina.jpg"
<template>
<img :src="getImageUrl(imageName)" />
</template>
<script>
export default {
methods: {
getImageUrl(name) {
return new URL(`/src/assets/${name}`, import.meta.url).href
}
}
}
</script>

Uncaught Error: Cannot find module 'file:/home/agemiro/backend-gerenciador/uploads/imagemteste.png'

I'm using React JS and I'm having trouble accessing an image that comes from a state/variable. If I put the url inside the require in the html img it works but when it comes from outside it doesn't work at all. I wish someone would clarify this because it's been four days since I've been trying to find out. Important: This url I'm using comes from outside the React application
const [sideDishesImage, setSideDishesImage] = useState([
{
fileName: "",
id: "",
message: "",
sideId: "",
url: "file:/home/agemiro/backend-gerenciador/uploads/imagemteste.png"
}
]);
<img {require(`${sideDishesImage[0].url}`)} alt="image" />
You have two ways to reference an asset in a create-react-app app:
path to the asset, the asset has to be placed in the public folder, and you can reference it like:
const url = "/images/myImage.png" // myImage.png must be inside "public/images"
<img src={url} alt="alt" />
Load the asset through webpack:
import img from "path/to/the/asset.png" // This can be outside of public folder but must be inside the /src folder
<img src={img} alt="alt" />

how to attach image using v-for in vue 3

im trying to make a set of images using v-for loop, but somehow it doesn't recognise variables inside the tag. pic is broken and alt just shows me {{ item.alt }} thing.
Here is my HTML:
<section class="our-technologies__section" v-for="(item, index) in tech_item" :key="index">
<div class="technologies__logo" #click="activeIndex = index" :class="{active: activeIndex === index}">
<img src="{{ item.src }}" alt="{{ item.alt }}">
<p>{{item.title}}</p>
</div>
</section>
And here are script:
export default {
name: "OurTechnologies",
data: function () {
return{
tech_item: [
{ title: 'Twilio', text: 'gfmds/lgkmlk/f', src: '../../images/twilio.png', alt: 'Twilio' },
{ title: 'Android', text: '12334356676', src: '../../images/android.png', alt: 'Android' },
],
activeIndex: 0,
}
},
}
I tried to use <img :src="item.src" :alt="item.alt"> and its still no results.
In vue cli you should use require to load the image :
<img :src="require(item.src)" :alt="item.alt"/>
<img :src="item.src" :alt="item.alt"> should work ™, but then I don't know enough about the environment you have set up. I quickly tried with a fresh project using vite and using the App component (so that it was root level), it didn't even need require for it to work. For this to work your images would be usually be in the ./src/assets/ directory, which they are not and I'm wondering if that's what is causing the problems, that the images are falling out of scope. You likely have a different setup, but I think you might able to use another option.
Place the images into the ./public/images/ folder. This will make them included as part of the bundle whether you're using dev/serve or build. Then reference them as /images/*.png without require or import. As long as you are serving the app at root level so that /images resolves correctly this should make it work. Otherwise it may need a bit of extra finessing to target the right directory.

How to encode a url segment in Angular 7 in the Template for an image url

I have the following bit of code which is my attempt at encoding the url:
<img src="{{ '/assets/images/animals/' + (data.animal.image | encodeURIComponent) }}" alt="" class="animal" />
I'm getting the error:
The pipe 'encodeURIComponent' could not be found
I'm assuming there is no such pipe, or I should be importing something somewhere.
What is the right way of doing this, preferably all in the template?
As requested, here is a sample of the data
data = { "animal" : { id: 1; image:"dog.png"; } }
You have a few different options:
This method uses CSS and sets the image as a background so it resize better.
<div [style.backgroundImage]="'url('+ image +')'" class="image"></div>
Use Angular sanitizer to make sure that the url does not contain dangerous script https://angular.io/api/platform-browser/DomSanitizer
Call a function to calculate the url, then it can run the encode you want.
<img [src]="getImageUrl(data.animal.image)" />

display variable html image

In Angular 2 code, I have defined the image as:
public rObj:any = {};
this.rObj.imgsrc ="http:/../.png";
Want to render this from my HTML:
<img src = {{rObj.imgsrc}}>
There is no display.Any help appreciated.Thanks!
Use one way binding on the src attribute. Any html attribute can be bound to.
... so for you this is what you are looking for.
<img [src]="rObj.imgsrc" />
Another example..
<a [href]="myHref"></a>
You are using the wrong data binding:
string binding: <elt attr="Hello World"/>
input (component → template): <elt [attr]="x"/>
event listener (template → component): <button (click)="someFunction"/>
two-way data binding: <elt [(attr)]="x"/> (not used a lot, use forms instead)
So you actually need here to write <img [src]="rObj.imgsrc"/>