1

I'm simply trying to render an image in a component. I can't seem to find a decent answer that works.

Can someone point me in the right direction?

Enviroment:

angular/cli 1.1.2

node 8.1.0

OS win x64

angular -v 4.2.5

What I have tired (HTML):

<img [src]={{imgUrl}} /> <img [src]="imgUrl" /> <img src={{imgUrl}} /> <img src="imgUrl" /> <img ng-src="imgUrl" /> <img src="assets/images/img.jpg" /> <!-- and a few more --> 

With .ts file like:

import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent implements OnInit { imgUrl = ""; constructor() {} ngOnInit() { this.imgUrl = 'assets/images/img.jpg'; // i have also tried // ~/assets/... // ./assets/... // etc } } 

Project structure:

src --app ----home ------ts-files --assets ----images ------img.jpg 

I have also tried moving the file from assets to the same ts folder, and some other places.

But I only get the same 404 error.

GET http://localhost:4200/assets/images/img.jpg 404 (Not Found)

UPDATE

.angular-cli.json content (autogenerated):

{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "project": { "name": "Test" }, "apps": [ { "root": "src", "outDir": "dist", "assets": [ "assets", "favicon.ico" ], "index": "index.html", "main": "main.ts", "polyfills": "polyfills.ts", "test": "test.ts", "tsconfig": "tsconfig.app.json", "testTsconfig": "tsconfig.spec.json", "prefix": "app", "styles": [ "styles.css" ], "scripts": [], "environmentSource": "environments/environment.ts", "environments": { "dev": "environments/environment.ts", "prod": "environments/environment.prod.ts" } } ], "e2e": { "protractor": { "config": "./protractor.conf.js" } }, "lint": [ { "project": "src/tsconfig.app.json" }, { "project": "src/tsconfig.spec.json" }, { "project": "e2e/tsconfig.e2e.json" } ], "test": { "karma": { "config": "./karma.conf.js" } }, "defaults": { "styleExt": "css", "component": {} } } 
10
  • 2
    (Not "sir" :-/). .angular-cli.json is the configuration file for angular-cli. You should be able to find it at the root of your project next to README.md and package.json. Commented Jul 3, 2017 at 19:39
  • 2
    Quick test: can you view the favicon? localhost:4200/favicon.ico Commented Jul 3, 2017 at 19:56
  • 1
    Also if i build the project ng build --prod I get an assets/images/img.jpg folder Commented Jul 3, 2017 at 20:09
  • 2
    As @stealththeninja noticed, ur angular.cli.json misses images. Try add it - "assets": [ "assets", "images" Commented Jul 3, 2017 at 20:13
  • 3
    @J.D. images is a subdirectory of assets so it's included, no need to add it unless it's sibling to assets. Commented Jul 3, 2017 at 20:25

3 Answers 3

3

Are you sure that <img src="/assets/images/img.jpg"> doesn't work? Note the leading '/' in it. When you're using the Angular CLI, if you run ng build it should create a dist directory in your project with the built static Web files. Can you confirm that the assets/images/img.jpg file is in your generated output from ng build?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, during the build the folder was created with the image. And I did try with and without a leading / and more.
0

Answer provided by @J.D. in the comments section.

In .angular-cli.json add the name of your directory containing images to assets. Ex: directory to add is images "assets": ["assets", "images" ]

As @stealththeninja noticed, ur angular.cli.json misses images. Try add it - "assets": [ "assets", "images"

Comments

-1

have you tried <img ng-src="{{imgUrl}}" /> https://docs.angularjs.org/api/ng/directive/ngSrc

1 Comment

The question is about Angular 4. You're linking to the documentation of AngularJS 1.x, which is another, different framework.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.