question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[attr.width.px] can't work in angular9

See original GitHub issue

Env

  • os: win10
  • browser: Chrome 80.0.3987.149(x64)
  • package.json
    "@angular/animations": "^9.0.7",
    "@angular/cdk": "^9.1.3",
    "@angular/common": "^9.0.7",
    "@angular/compiler": "^9.0.7",
    "@angular/core": "^9.0.7",
    "@angular/forms": "^9.0.7",
    "@angular/localize": "^9.0.7",
    "@angular/material": "^9.1.3",
    "@angular/material-moment-adapter": "^9.1.3",
    "@angular/platform-browser": "^9.0.7",
    "@angular/platform-browser-dynamic": "^9.0.7",
    "@angular/router": "^9.0.7"

Desc

<td *ngIf="hasContent(r, c)"
    [attr.width.px]="model?.cells[r][c]?.width"
    [attr.height.px]="model?.cells[r][c]?.height + (isEditableMode() && last ? 1 : 0)"

Browser Display

Rendering failed because the property was compiled to width.px

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pkozlowski-opensourcecommented, Mar 26, 2020

tl;dr; this is not the valid syntax and it just “happened to work” in previous versions of Angular. As @DreamLi1314 suggested you need to use one of:

  • [style.width.px]="expression" if you want to bind to the CSS style property (probably this is what you want);
  • [att.width]="expression + 'px'" if you want to bind to the width attribute

What was happening with the VE attribute parser that any suffix would be dropped without any warning ( https://stackblitz.com/edit/angular-9qzvjk) which isn’t greatest user experience… I’m going to keep this issue open so we can decide what to do here (probably error when detecting such usage pattern).

1reaction
DreamLi1314commented, Mar 26, 2020

[style.width.px]="model?.cells[r][c]?.width" and [attr.width]="model?.cells[r][c]?.width" is work for me. But I think this should be a mistake or why doesn’t this work in Angular9?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to bind a property to style.width in pixels? - Stack Overflow
Press F12 or hit ctrl + shift + i to open Inspect. Go to Elements Tab and find you div and check it's...
Read more >
NgStyle - Angular
An attribute directive that updates styles for the containing HTML element. Sets one or more style properties, specified as colon-separated key-value pairs.
Read more >
CSS values and units - Learn web development | MDN
The first box has a width set in pixels. As an absolute unit, this width will remain the same no matter what else...
Read more >
HTML video width Attribute - W3Schools
The width attribute specifies the width of a video player, in pixels. Tip: Always specify both the height and width attributes for videos....
Read more >
Set a inline dynamic css width attribute with angular expression
Hi All, I have come across a handy directive to allow inline style if the angular condition is true. This will fix any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found