angular - 如何有条件地将 div 包裹在 ng-content 周围

标签 angular angular-ng-if angular2-ngcontent

根据( bool )类变量的值,我希望我的 ng-content 要么包装在 div 中,要么不包装在 div 中(即 div 甚至不应该是在 DOM 中)...最好的方法是什么?我有一个Plunker试图做到这一点,以我认为是最明显的方式,使用 ngIf ..但它不起作用...它仅显示其中一个 bool 值的内容,而不显示另一个 bool 值的内容

请帮忙 谢谢!

http://plnkr.co/edit/omqLK0mKUIzqkkR3lQh8

@Component({
  selector: 'my-component',
  template: `

   <div *ngIf="insideRedDiv" style="display: inline; border: 1px red solid">
      <ng-content *ngIf="insideRedDiv"  ></ng-content> 
   </div>

   <ng-content *ngIf="!insideRedDiv"></ng-content>     

  `,
})
export class MyComponent {
  insideRedDiv: boolean = true;
}


@Component({
  template: `
    <my-component> ... "Here is the Content"  ... </my-component>
  `
})
export class App {}

最佳答案

Angular ^4

作为解决方法,我可以为您提供以下解决方案:

<div *ngIf="insideRedDiv; else elseTpl" style="display: inline; border: 1px red solid">
  <ng-container *ngTemplateOutlet="elseTpl"></ng-container>
</div>

<ng-template #elseTpl><ng-content></ng-content> </ng-template>

<强> Plunker Example angular v4

Angular <4

在这里您可以创建专用指令来执行相同的操作:

<div *ngIf="insideRedDiv; else elseTpl" style="display: inline; border: 1px red solid">
   <ng-container *ngTemplateOutlet="elseTpl"></ng-container>
</div>

<template #elseTpl><ng-content></ng-content></template>

<强> Plunker Example

ngIf4.ts

class NgIfContext { public $implicit: any = null; }

@Directive({ selector: '[ngIf4]' })
export class NgIf4 {
  private context: NgIfContext = new NgIfContext();
  private elseTemplateRef: TemplateRef<NgIfContext>;
  private elseViewRef: EmbeddedViewRef<NgIfContext>;
  private viewRef: EmbeddedViewRef<NgIfContext>;

  constructor(private viewContainer: ViewContainerRef, private templateRef: TemplateRef<NgIfContext>) { }

  @Input()
  set ngIf4(condition: any) {
    this.context.$implicit = condition;
    this._updateView();
  }

  @Input()
  set ngIf4Else(templateRef: TemplateRef<NgIfContext>) {
    this.elseTemplateRef = templateRef;
    this.elseViewRef = null;
    this._updateView();
  }

  private _updateView() {
    if (this.context.$implicit) {
      this.viewContainer.clear();
      this.elseViewRef = null;

      if (this.templateRef) {
        this.viewRef = this.viewContainer.createEmbeddedView(this.templateRef, this.context);
      }
    } else {
      if (this.elseViewRef) return;

      this.viewContainer.clear();
      this.viewRef = null;

      if (this.elseTemplateRef) {
        this.elseViewRef = this.viewContainer.createEmbeddedView(this.elseTemplateRef, this.context);
      }
    }
  }
}

关于angular - 如何有条件地将 div 包裹在 ng-content 周围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47639034/

相关文章:

Angular 2 渲染一个完全没有任何包装标签的组件

angular - 使用 ng-content 时,表单验证不考虑输入

Angular 8 - POST + 重定向与提交 HTML <form> 完全一样,但不使用 DOM

javascript - Angular 指令 ng-if 不评估条件语句

html - 创建可在各种页面上使用的可重用模板

angular - Angular中改变某个组件布局的宽度

angular - 如何循环使用部分元素以使用 ng-content 进行投影

angular - Javascript removeEventListener OnDestroy 在 Angular 6 中不起作用

css - PrimeNG 元素未限定范围,无法使用默认的 Angular 2 ViewEncapsulation(模拟)设置样式

html - 风格的动态变化