19

My goal is to set a style (height and width in this case) from a component variable using the "styles" attribute. I would think there is a simple data binding method but that may be wishful thinking...

For example if I were using the html mustache binding it might look like this:

@Component({
    selector: '[sidebar]',
    templateUrl: 'app/Nav/sidebar.comp.html',
    styles: [`
        .sidebar-nav {
            overflow: scroll;
            height: {{ height }};
        }
        .sidebar {
            height: {{ 0.9 * height }};
            width: {{ 0.21 * width }};
        }
    `]
})

export class SidebarComp {
    width: number;
    height: number;

    constructor() {
        this.height = window.innerHeight;
        this.width = window.innerWidth;
    }
}

Obviously this is all wrong but I've tried some more likely permutations and had no luck finding solutions on the Angular site, Stack Overflow, or Google. I may be reduced to using ngStyle inline but that's not ideal in this case.

1
  • You don't show what you want to style. Is .sidebar the SidebarComp itself or something inside sidebar.comp.html? Commented Jun 30, 2016 at 18:37

3 Answers 3

27

You can style the host element like

@Component({
  selector: '[sidebar]',
  templateUrl: 'app/Nav/sidebar.comp.html',
  host: {
    '[style.height.px]':'0.9 * height',
    '[style.width.px]':'0.21 * width'
  }

})

export class SidebarComp {
    width: number;
    height: number;

    constructor() {
        this.height = window.innerHeight;
        this.width = window.innerWidth;
    }
}

and the content (app/Nav/sidebar.comp.html) like

<div class="sidebar-nav" [style.overflow]="'scroll'" [style.height.px]="height">

or

<div class="sidebar-nav" [ngStyle]="{overflow: 'scroll', height: height + 'px'}">
Sign up to request clarification or add additional context in comments.

6 Comments

And in PsudoClass?
@AlejoNext sorry, no idea what you mean
::before how edit the style? in typescript or javascript
I'm pretty sure that's not supported
@GünterZöchbauer where did you find the documention for this?
|
4

If you are using percent (%) try this :

<div class="sidebar-nav"   [style.height.%]="height">

Comments

1

In my case, I need change image style dynamic switch between px and % unit.

So I use [ngStyle] instead of [style.height.px]

I share for whom concern.

HTML

<img [src]="imageUrl | safe: 'url'" [ngStyle]="{width: imagewidth, height: imageheight}">

TS

    if (this.properties.imageStyle == 'Stretch') {
      this.imagewidth = this.cols * this.unitWidth + 'px';
      this.imageheight = this.rows * this.unitHeight + 'px';
    }

    if (this.properties.imageStyle == 'Clip') {
      this.imagewidth = 100 + '%';
      this.imageheight = 100 + '%';
    }

https://stackblitz.com/edit/angular-image-clip-stretch

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.