简体   繁体   English

如何向动态创建的组件添加样式

[英]How to add styles to dynamically created component

I'm trying to set styles to dynamically created components. 我正在尝试将样式设置为动态创建的组件。

I want to do something like this, because what i want can't be done with s/css styles (class or id). 我想做这样的事情,因为我想做的事情不能用s / css样式(类或id)完成。

<div [style.height]="myVar">

The components that i'm creating are: 我正在创建的组件是:

let widgetResourceGroupFactory = this.resolver.resolveComponentFactory(ResourceGroupComponent);

for (let group of this.groups) {
  let widgetResourceGroupReference = this.content.createComponent(widgetResourceGroupFactory);  

  widgetResourceGroupReference.instance['group'] = group;
  widgetResourceGroupReference.instance['slug'] = group.slug;

  this.renderer.listen(widgetResourceGroupReference.location.nativeElement, 'click', () => {
    this.loadCategories(group);
  });
}

I want to set a custom style to widgetResourceGroupReference , but what i was trying didn't work, that was a sort of instance['style.height'] . 我想为widgetResourceGroupReference设置自定义样式,但是我尝试的方法不起作用,那是一种instance['style.height']

Thanks u very much. 非常感谢你。

好吧,我用渲染器解决了它:

this.renderer.setElementStyle(widgetResourceGroupReference.location.nativeElement, 'width', this.upperLeftWidth);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM