如何在同一个组件中多次使用 ng-content?

huangapple go评论52阅读模式
英文:

how to use ng-content multiple in same component

问题

如何在同一个组件中多次使用 ng-content?我目前已成功地将其用于开始时间,但当我添加结束时间时,开始时间的值也被传递到结束时间的值。你能帮我解决这个问题吗?

这是我的主组件:

<div class="p-float-label">
    <p-calendar [(ngModel)]="startTimeAsDate" formControlName="startTime"
      [inputId]="'startTime_' + day + '_' + inputIndex" (ngModelChange)="checkStartTimeValidity()" [showTime]="true"
      [timeOnly]="true"><p-footer><ng-content select="[id=startTime]"></ng-content></p-footer></p-calendar>
    <label [for]="'startTime' + inputIndex">{{ 'startTime' | translate }}</label>
</div>

<div class="p-float-label">
    <p-calendar [(ngModel)]="endTimeAsDate" formControlName="endTime" [inputId]="'endTime_' + day + '_' + inputIndex"
      (ngModelChange)="checkEndTimeValidity()" [showTime]="true" [timeOnly]="true"><p-footer><ng-content
          select="[id=endTime]"></ng-content></p-footer></p-calendar>
    <label [for]="'endTime' + inputIndex">{{ 'endTime' | translate }}</label>
</div>

这是我的第二个组件:

<app-start-time-end-time [footer]="footer" [day]="day" [inputIndex]="i"
          [isMinusButtonVisible]="plusMinusButtonVisibilities[i].isMinusButtonVisible"
          [isPlusButtonVisible]="plusMinusButtonVisibilities[i].isPlusButtonVisible"
          (minusButtonClicked)="removeTimeValueFromArray($event)" (plusButtonClicked)="addNewTimeValueToArray()"
          [(endTime)]="timeValues[i].endTime!" [(startTime)]="timeValues[i].startTime!">
          <span id="startTime"><ng-content></ng-content></span>
          <span id="endTime"><ng-content></ng-content></span>
</app-start-time-end-time>

这是我的第三个组件:

<app-week-day-time [footer]="getFooterContent()" [(timeValues)]="mondayTimeValues"
        [dayCheckboxLabel]="translate('offDay')" [day]="'monday'">
        <span id="startTime" class="clickToApllyStart clickable clickable cursor-pointer text-blue-500 w-3 pl-3"
          (click)="onApplyToAllStartTimesClicked(mondayTimeValues[0].startTime, 0)">Apply to All Start Times</span>
</app-week-day-time>

我尝试在同一个组件中多次使用 ng-content。我希望分别为结束时间和开始时间添加不同的值,但目前开始时间的值也传递到结束时间。如何确保每个 ng-content 区块都有自己独立的 startTime 和 endTime 值?

英文:

How can I use ng-content multiple times in the same component? I'm currently using it successfully for the start time, but when I add the end time, the start time values are being passed to the end time values. Can you help me fix this?

this is my main component

`  &lt;div class=&quot;p-float-label&quot;&gt;
        &lt;p-calendar [(ngModel)]=&quot;startTimeAsDate&quot; formControlName=&quot;startTime&quot;
          [inputId]=&quot;&#39;startTime_&#39;+day+&#39;_&#39; + inputIndex&quot; (ngModelChange)=&quot;checkStartTimeValidity()&quot; [showTime]=&quot;true&quot;
          [timeOnly]=&quot;true&quot;&gt;&lt;p-footer&gt;&lt;ng-content select=&quot;[id=startTime]&quot;&gt;&lt;/ng-content&gt;&lt;/p-footer&gt;&lt;/p-calendar&gt;
        &lt;label [for]=&quot;&#39;startTime&#39; + inputIndex&quot;&gt;{{&#39;startTime&#39; | translate}}&lt;/label&gt;
      &lt;/div&gt;


&lt;div class=&quot;p-float-label&quot;&gt;
        &lt;p-calendar [(ngModel)]=&quot;endTimeAsDate&quot; formControlName=&quot;endTime&quot; [inputId]=&quot;&#39;endTime_&#39;+day+&#39;_&#39; + inputIndex&quot;
          (ngModelChange)=&quot;checkEndTimeValidity()&quot; [showTime]=&quot;true&quot; [timeOnly]=&quot;true&quot;&gt;&lt;p-footer&gt;&lt;ng-content
              select=&quot;[id=endTime]&quot;&gt;&lt;/ng-content&gt;&lt;/p-footer&gt;&lt;/p-calendar&gt;
        &lt;label [for]=&quot;&#39;endTime&#39; + inputIndex&quot;&gt;{{&#39;endTime&#39; | translate}}&lt;/label&gt;
      &lt;/div&gt;

`

this is my second component

`   &lt;app-start-time-end-time [footer]=&quot;footer&quot; [day]=&quot;day&quot; [inputIndex]=&quot;i&quot;
          [isMinusButtonVisible]=&quot;plusMinusButtonVisibilities[i].isMinusButtonVisible&quot;
          [isPlusButtonVisible]=&quot;plusMinusButtonVisibilities[i].isPlusButtonVisible&quot;
          (minusButtonClicked)=&quot;removeTimeValueFromArray($event)&quot; (plusButtonClicked)=&quot;addNewTimeValueToArray()&quot;
          [(endTime)]=&quot;timeValues[i].endTime!&quot; [(startTime)]=&quot;timeValues[i].startTime!&quot;&gt;
          &lt;span id=&quot;startTime&quot;&gt;&lt;ng-content&gt;&lt;/ng-content&gt;&lt;/span&gt;
          &lt;span id=&quot;endTime&quot;&gt;&lt;ng-content&gt;&lt;/ng-content&gt;&lt;/span&gt;
        &lt;/app-start-time-end-time&gt;`

this is my third component

`&lt;app-week-day-time [footer]=&quot;getFooterContent()&quot; [(timeValues)]=&quot;mondayTimeValues&quot;
        [dayCheckboxLabel]=&quot;translate(&#39;offDay&#39;)&quot; [day]=&quot;&#39;monday&#39;&quot;&gt; &lt;span id=&quot;startTime&quot;
          class=&quot;clickToApllyStart clickable clickable cursor-pointer text-blue-500 w-3 pl-3&quot; (click)=&quot;
        onApplyToAllStartTimesClicked(mondayTimeValues[0].startTime, 0)
      &quot;&gt;Apply to All Start Times&lt;/span&gt;
      &lt;/app-week-day-time&gt;`

I'm trying to use ng-content multiple times in the same component. I want to add different values for the end time and start time separately, but currently, the values of the start time are being passed to the end time as well. How can I ensure that each ng-content block has its own separate values for startTime and endTime?

答案1

得分: 0

ng-content 可以用于多槽内容投影。为此,您需要向<ng-content>元素添加一个select属性。

<ng-content select="[question]"></ng-content>

其中select值是一个CSS选择器。

英文:

ng-content can be used for multi-slot content projection. For this you'll need to add a select attribute to the <ng-content> elements.

&lt;ng-content select=&quot;[question]&quot;&gt;&lt;/ng-content&gt;

Where the select value is a css selector.

huangapple
  • 本文由 发表于 2023年7月18日 02:24:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定