Wagner Moreira
Wagner Moreira

Reputation: 1088

env vars in Angular 4

.element {
  background: url(env.asset-path)
}

What I want is to detect the env at the Angular side and define the asset path in CSS

I know that I could check server-name/domain with js but I don't know how to pass variables to css/scss files

I'm using Angular4/Typescript/scss

Upvotes: 0

Views: 68

Answers (1)

FussinHussin
FussinHussin

Reputation: 1814

you can get your environment variable by importing it, the same way as your main.ts file:

import { enableProdMode } from '@angular/core';

myurl = environment.production

you cannot add these vars to your css files, but you can manipulate the dom, and add the background url by binding to the src attribute or doing an angular attribute binding. 1)

<img [src]="myurl">

2)

<div class="background"
     [ngStyle]="{ 'background-image': 'url(myurl)'}">

Upvotes: 1

Related Questions