【问题标题】:Implementing your own *ngIf实现你自己的 *ngIf
【发布时间】:2017-09-15 11:01:41
【问题描述】:

我想创建自己的 *ngIf 指令。该指令将获得一个数字,如果屏幕内宽像素小于它获得的数字,它将不会呈现模板。

如何实现自己的 *ngIf?

【问题讨论】:

  • 用css试试这个@media only screen and (max-width: 768px){
  • 请把你的问题说得更清楚。

标签: angular angular2-directives angular-ng-if elementref


【解决方案1】:

称为结构指令。

您可以查看source of *ngIf

Angular 文档中也有一节关于 https://angular.io/guide/structural-directives

主要部分是

 this.viewContainer.createEmbeddedView(this.templateRef);

允许标记模板。

如果你有

<my-component *myIf="isTrueOrFalse"></my-component>

* 将导致

<my-component></my-component>

作为模板传递给指令,然后可以标记/删除。

【讨论】:

    【解决方案2】:

    这很简单。

    使用旧的ngIf,就像这个*ngIf='someVariable',然后当你希望它被渲染或不渲染时,只需将这个someVariabletrue切换到false

    我要做的是在组件中创建一个事件侦听器,用于侦听窗口宽度事件并查看宽度将通过设置 someVariable 来决定是否隐藏某些元素

    【讨论】:

      猜你喜欢
      • 2013-12-05
      • 2021-12-15
      • 2017-02-22
      • 2016-05-02
      • 2012-02-12
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 2017-09-19
      相关资源
      最近更新 更多