43

取决于(布尔)类变量的值,我希望我ng-content要么被包裹在 div 中,要么不被包裹在 div 中(即 div 甚至不应该在 DOM 中)...... 最好的方法是什么对这个 ?我有一个Plunker试图做到这一点,我认为这是最明显的方式,使用 ngIf .. 但它不起作用......它只显示一个布尔值的内容,而不显示另一个

请协助谢谢!

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 {}
4

3 回答 3

79

角度^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 示例 angular v4

角度 < 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 示例

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);
      }
    }
  }
}
于 2017-01-18T19:09:44.640 回答
7

请记住,您可以将所有这些逻辑放在单独的组件中!(基于 yurzui 的回答):

import { Component, Input } from '@angular/core';

@Component({
    selector: 'div-wrapper',
    template: `
    <div *ngIf="wrap; else unwrapped">
      <ng-content *ngTemplateOutlet="unwrapped">
      </ng-content>
    </div>
    <ng-template #unwrapped>
      <ng-content>
      </ng-content>
    </ng-template>
    `,
})
export class ConditionalDivComponent {
  @Input()
  public wrap = false;
}

然后你可以像这样使用它:

<div-wrapper [wrap]="'true'">
 Hello world!        
</div-wrapper>
于 2017-05-23T10:30:15.363 回答
2

我对此进行了检查,并发现了一个关于标签的多个嵌入主题的未解决问题。这可以防止您在单个模板文件中定义多个标签。

这解释了为什么只有在您的 plunker 示例中删除其他标签时才能正确显示内容。

您可以在此处查看未解决的问题: https ://github.com/angular/angular/issues/7795

于 2017-01-11T15:13:57.500 回答