Henkolicious
Henkolicious

Reputation: 1401

Angular 2/4 img src

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": {}
  }
}

Upvotes: 1

Views: 7686

Answers (3)

Kostas Bastas
Kostas Bastas

Reputation: 11

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

Upvotes: -1

ob1
ob1

Reputation: 1434

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"

Upvotes: 0

SaxyPandaBear
SaxyPandaBear

Reputation: 377

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?

Upvotes: 3

Related Questions