我的目标是使用"styles"属性从组件变量设置样式(在这种情况下为高度和宽度).我认为有一种简单的数据绑定方法,但这可能是一厢情愿的想法......
例如,如果我使用html胡子绑定,它可能看起来像这样:
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
显然这是错的,但我尝试了一些更可能的排列,并且没有在Angular网站,Stack Overflow或Google上找到解决方案.我可以简化为使用ngStyle内联,但在这种情况下这并不理想.
Gün*_*uer 24
你可以设置主机元素的样式
@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;
}
}
Run Code Online (Sandbox Code Playgroud)
和content(app/Nav/sidebar.comp.html)一样
<div class="sidebar-nav" [style.overflow]="'scroll'" [style.height.px]="height">
Run Code Online (Sandbox Code Playgroud)
要么
<div class="sidebar-nav" [ngStyle]="{overflow: 'scroll', height: height + 'px'}">
Run Code Online (Sandbox Code Playgroud)