Angular2 Init and destroy component

时间:2017-07-12 08:08:15

标签: javascript angular

Developers. There's a problem with <ng-content></ng-content>. I have created a component and it look's like this. But component <boat></boat> <competitorEvent ></competitorEvent > and <waypoint ></waypoint> initializes one time and don not get destroyed

<tabs>
  <tab number="{{event?.boats?.length}}" title="{{'CATEGORY.BOATS' | translate}}" icon="directions_boat">
    <boat [data]=event?.boats></boat>
  </tab>
  <tab number="{{event?.competitors?.length}}" title="{{'CATEGORY.COMPETITOR' | translate}}" icon="people">
    <competitorEvent [data]="event?.competitors"></competitorEvent>
  </tab>
  <tab number="{{event?.wayPoints?.length}}" title="{{'CATEGORY.WAYPOINTS' | translate}}" icon="place">
    <waypoint [distances]="event?.distances"></waypoint>
  </tab>
</tabs>

tabs.ts

@Component({
    selector: "tabs",
    template: `
    <div class="ui-g">
      <ng-container *ngFor="let tab of tabs" >
        <div class="ui-g-12 ui-md-2 ui-lg-2" (click)="selectTab(tab)">
        <a>
          <div class="ui-g card colorbox colorbox-1" [class.active]="tab.pressed">
            <div class="ui-g-4" *ngIf="tab.icon">
              <i class="material-icons">{{tab.icon}}</i>
            </div>
            <div class="ui-g-8">
              <div class="colorbox-name truncate">{{tab.title}}</div>
              <div class="colorbox-count">{{tab.number}}</div>
            </div>
          </div>
        </a>
      </div>
      </ng-container>
    </div>
    <ng-content ></ng-content>
  `
})

tab.ts

@Component({
    selector: "tab",
    template: `
    <ng-template class="container"  *ngIf="pressed">
      <ng-content *ngIf="pressed"></ng-content>
    </ng-template>
  `
})

In this code when you click each component launches component lifecycle everytime

<div [ngSwitch]="tab">
  <div *ngSwitchCase="1">
    <boat [data]=event?.boats></boat>
  </div>
  <div *ngSwitchCase="2">
    <competitorEvent [data]="event?.competitors"></competitorEvent>
  </div>
  <div *ngSwitchCase="3">
    <waypoint [distances]="event?.distances"></waypoint>
  </div>
  </div>

1 个答案:

答案 0 :(得分:0)

update Angular 5

ngOutletContext was renamed to ngTemplateOutletContext

See also https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29

original

<ng-content *ngIf="pressed"></ng-content>

doesn't create or destroy the content projected through them, it's just a marker where child content should be displayed (projected to).

The content stays part of the component where it's actually added.

To have more control you could pass <template><boat></boat></template> and add it using ngTemplateOutlet (or similar) in TabComponent.

Another alternative would be

<tab number="{{event?.boats?.length}}" title="{{'CATEGORY.BOATS' | translate}}" icon="directions_boat" #tab>
    <boat *ngIf="tab.visible" [data]=event?.boats></boat>

where TabComponent provides a property visible that the child uses to get itself added/removed depending on the value of visible.