(EDITED) I had a simple jarallax with just a div with the correspondent configuration in a component:
<div class="view cover-jarallax" data-jarallax="{"speed": '0.w'}"
style="background-image: url(imgurl);' +
'height: ' + this.height + ';"></div>
I need to use it one or two more times, and so I'm trying to make it dynamic, receiving the speed, url and height as an @Input from the parent component.
The thing is that I'm having some issues doing that, due to using a dynamic binding directly to the div's attributes. I mean:
<div class="view cover-jarallax" [data-speed]="datajarallax"
[ngStyle]="style"></div>
And in the component:
import {Component, Input, OnInit} from '@angular/core';
@Component({
selector: 'app-middle-parallax',
templateUrl: './middle-parallax.component.html',
styleUrls: ['./middle-parallax.component.scss']
})
export class MiddleParallaxComponent implements OnInit {
@Input() speed: number;
@Input() imgURL: string; // time to wait after finish typing to start
deleting the current string
@Input() height: string;
style = '';
datajarallax = '';
constructor() { }
ngOnInit() {
this.style = 'background-image: url(\'' + this.imgURL + '\');' +
'height: ' + this.height + '; ' +
'background-repeat: no-repeat; ' +
'background-size: cover; ' +
'background-position: center center;';
this.datajarallax = '{"speed": ' + this.speed + '}';
}
}
That's not working, I supose that is because of that I just said, but It doesn't come to me the way to go. Any help?
EDIT: The console shows this error:
ERROR Error: Cannot find a differ supporting object 'background-image:
url('https://images.unsplash.com/photo-1484417894907-623942c8ee29?ixlib=
rb-0.3.5&s=db802b72adc82f97904d37dde9d0d401&dpr=1&auto=format&fit=crop&w=1000&q
=80&cs=tinysrgb');height: 400px; background-repeat: no-repeat;
background-size: cover; background-position: center center;'
SOLUTION (STYLES):
I had to pass the style string as an object to ngStyle:
this.style = {'background-image': 'url(\'' + this.imgURL + '\')' ,
'height': this.height,
'background-repeat': 'no-repeat',
'background-size': 'cover',
'background-position': 'center center'};
EDIT2: Now styles are binding but data-jarallax doesn't, neither as object or as string
<div class="view cover-jarallax" [data-jarallax]="datajarallax"
[ngStyle]="style"></div>
.
this.datajarallax = {'speed': 0.2};
this.datajarallax = '{\'speed\': 0.2}';
data-jarallax="{{datajarallax}}"and setdatajarallaxto{"speed": this.height}