如何将一个属性绑定到style.width以像素为单位? [英] How to bind a property to style.width in pixels?
本文介绍了如何将一个属性绑定到style.width以像素为单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Angular 2,并且编写了以下代码:
I'm using Angular 2 and I've written the below code:
<div [style.width.px]="width">some texts </div>
我也尝试过:
<div [ngStyle]="{'width.px': width}">some texts </div>
export class MyComponent
{
width: number = 150;
}
但是它不会将宽度绑定到div
元素.
But it doesn't bind the width to the div
element.
我在做什么错了?
推荐答案
对我来说很好
@Component({
selector: 'my-app',
styles: [`div { border: 3px solid red; }`]
template: `
<div>
<h2>Hello {{name}}</h2>
<div [style.width.px]="width">some texts </div>
</div>
`,
})
export class App {
name:string;
width: number = 250;
constructor() {
this.name = 'Angular2'
}
}
这篇关于如何将一个属性绑定到style.width以像素为单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文