typescript - Angular 2 : How to set-up a @Input for an inline style? -


i have component needs display random values, randomly generated , pushed in trough @input bindings troughout template. far good, can't seem link @input style image url so:

<a routerlink="{{nextlink}}" routerlinkactive="active" class="nav-link next" style="background: url('assets/images/{{nextbg}}');">

where {{nextbg}} file extension e.g. next.jpg => this aiming for.

i tried using [ngstyle] , [style.background-image] without luck.

here code:

import { component, input } '@angular/core';  @component({     moduleid: module.id,     selector: 'prev-next',     template: `     <nav class="prev-next-nav">         <a routerlink="{{prevlink}}" routerlinkactive="active" class="nav-link prev" style="background: url('assets/images/tesla.jpg');">             <span class="link-dir">previous project</span>             <span class="link-title"><i class="icon icon-arrowright"></i>&nbsp;{{prevname}}</span>         </a>         <a routerlink="{{nextlink}}" routerlinkactive="active" class="nav-link next" style="background: url('assets/images/food.jpg');">             <span class="link-dir">next project</span>             <span class="link-title">{{nextname}}&nbsp;<i class="icon icon-arrowright"></i></span>         </a>     </nav> ` })  export class prevnextcomponent {     // previous link inputs     @input() prevbg: string;     @input() prevname: string;     @input() prevlink: string;      // next link inputs     @input() nextbg: string;     @input() nextname: string;     @input() nextlink: string; } 

try following using [ngstyle]:

<a routerlink="{{prevlink}}" routerlinkactive="active" class="nav-link prev" [ngstyle]="{'background-image': 'url(assets/images/' + prevbg + ')'}">     <span class="link-dir">previous project</span>     <span class="link-title"><i class="icon icon-arrowright"></i>&nbsp;{{prevname}}</span> </a> 

or perhaps utilize oninit lifecycle hook build out urls within component reference @input() prevbg: string;.

html

<a routerlink="{{prevlink}}" routerlinkactive="active" class="nav-link prev" [ngstyle]="{'background-image': prevbgurl}">     <span class="link-dir">previous project</span>     <span class="link-title"><i class="icon icon-arrowright"></i>&nbsp;{{prevname}}</span> </a> 

ts

ngoninit() {     this.nextbgurl = `url(assets/images/${this.nextbg})`; } 

i've created plunker. if click on 1 of hero's you'll see placehold.it image being loaded in div.foo. should load local asset url in same say.

hopefully helps!


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -