首页 > 解决方案 > 在另一个指令中使用指令

问题描述

如何在另一个指令定义中插入指令规则?我已经定义了这个指令:

@Directive({
  selector: "[appCardBody]"
})
export class CardBodyDirective {
  constructor(el: ElementRef) {
    el.nativeElement.style.width = "95%";
    el.nativeElement.style.marginTop = "20px";
  }
}

我以这种方式使用这个指令:

<div
   appCardBody
   fxLayout="row"
   fxLayoutAlign="space-between center"
   fxLayout.lt-md="column"></div>

如何在我的 appCardBody 指令中直接包含 fx* 属性(来自 angular flex-layout 指令)?

我已经尝试过强制这样的元素:

@Directive({
  selector: "[appCardBody]"
})
export class CardBodyDirective {
  constructor(el: ElementRef) {
    el.nativeElement.style.width = "95%";
    el.nativeElement.style.marginTop = "20px";
    el.nativeElement.fxLayout = "row"
    // the other proprieties
  }
}

但是这样是行不通的。有什么建议吗?

标签: angular-directiveangular7

解决方案


推荐阅读