Angular 8:检测 ng-content 中是否有内容(或存在)

Ste*_*eve 10 javascript angular

我有一个组件,其模板允许 2 个内容区域:文本和“阅读更多”文本。如果组件的使用者为“阅读更多”文本添加区域,我想显示最终用户单击以显示文本的“阅读更多”链接。如果他们不包含/不需要任何“阅读更多”文本,我不想显示链接。

如何检测模板区域的存在,并根据ngIf?

例如,html 可能是:

<app-promohero-message-unit title="Title for messaging module">
     <div description>
       Include a short, informative description here.
     </div>
     <div readmoretext>
       If you need to add more detail, include another sentence or two it in this section.
     </div>
</app-promohero-message-unit>
Run Code Online (Sandbox Code Playgroud)

显然,他们可能不需要readmoretext,所以如果他们省略了它,我不应该显示 readmore 链接。

到目前为止,组件代码是:

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

@Component({
  selector: 'app-promohero-message-unit',
  template: `
    <div>
      <h3 class="text-white">{{ title }}</h3>
      <p class="text-white">
        <ng-content select="[description]"></ng-content>
      </p>
      <p class="text-white" *ngIf="readMore">
        <ng-content select="[readmoretext]"></ng-content>
      </p>
    </div>
    <p>
      <a class="text-white" (click)="showReadMore()" *ngIf="something"><u>Read more</u></a>
    </p>
  `
})
export class PromoheroMessageUnitComponent {
  @Input()
  title: string;

  readMore = false;

  showReadMore() {
    this.readMore = true;
  }
}
Run Code Online (Sandbox Code Playgroud)

Udi*_*zor 9

在 Angular 8 中,您不必使用 ngAfterViewInit 生命周期挂钩。只要将 viewchild 的“static”值设置为 true,就可以使用 ngOnInit。

import { Component, OnInit, ViewChild, TemplateRef, ElementRef } from '@angular/core';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.scss']
})
export class TestComponent implements OnInit {
  @ViewChild('content', { read: ElementRef, static: true }) content: ElementRef;
  constructor() { }

  ngOnInit() {
    console.log(!!this.content.nativeElement.innerHTML);  // return true if there is a content
  }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您必须使用 html 标签(例如 div、span 等)包装 ng-content 指令,并在此外部标签上设置 templateRef。

<div #content>
  <ng-content></ng-content>
</div>
Run Code Online (Sandbox Code Playgroud)

我把它放在 stackblitz 上:https://stackblitz.com/edit/angular-8-communicating- Between-components-mzneaa?file=app/app.component.html


asi*_*hmi 5

你可以得到一个参考ng-content模板变量),然后访问在你的分量变量来检查的,内容长度ng-content使用ViewChild

然后你可以使用ngAfterViewInit生命周期钩子来检查ng-content长度

您的代码将是这样的:

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

@Component({
  selector: 'app-promohero-message-unit',
  template: `
    <div>
      <h3 class="text-white">{{ title }}</h3>
      <p class="text-white">
        <ng-content select="[description]"></ng-content>
      </p>
      <p class="text-white" *ngIf="readMore">
        <ng-content #readMoreContent select="[readmoretext]"></ng-content>
      </p>
    </div>
    <p>
      <a class="text-white" (click)="showReadMore()" *ngIf="something"><u>Read more</u></a>
    </p>
  `
})
export class PromoheroMessageUnitComponent {
  @Input()
  title: string;
  @ViewChild('readMoreContent') readMoreContent: ElementRef;

  readMore = false;

  ngAfterViewInit() {
    if (this.readMoreContent.nativeElement.childNodes.length.value == 0){
      this.readMore = false
    }
  }

  showReadMore() {
    this.readMore = true;
  }
}
Run Code Online (Sandbox Code Playgroud)