Search code examples
angularbindingionic2

Angular2 switch div content


I am wanting to switch the content of two div in angular2/ionic2 this is my code. It does switch the html but I seem to loose all binding to the elements.

<ion-list>
<div #currencyFromHtml>
  <ion-row>
    <ion-col col-8>
      <ion-item>
        <ion-input [(ngModel)]="currencyFromAmount" (ionChange)="refreshConversion()"></ion-input>
      </ion-item>
    </ion-col>
    <ion-col col-4>
      <ion-item>
          <ion-select [(ngModel)]="currencyFrom"  (ionChange)="refreshConversion()">
            <ion-option *ngFor="let c of coins; let i = index" [selected]="i==0" [value]="c">{{c.title}}</ion-option>
          </ion-select>
      </ion-item>
    </ion-col>
  </ion-row>
</div>
<ion-row>
        <button ion-button (click)="switchToAndFromCurrency()">Switch</button>
  </ion-row>
<div #currencyToHtml>
<ion-row>
    <ion-col col-8>
      <ion-item>
        <ion-input [(ngModel)]="currencyToAmount"  (ionChange)="refreshConversion()"></ion-input>
      </ion-item>
    </ion-col>
    <ion-col col-4>
    <ion-item>
        <ion-select [(ngModel)]="currencyTo" (ionChange)="refreshConversion()">
          <ion-option *ngFor="let cur of currency; let i = index" [selected]="i==0" [value]="cur">{{cur.title}}</ion-option>
        </ion-select>
    </ion-item>
  </ion-col>
  </ion-row>
</div>

The code that runs is this:

    switchToAndFromCurrency()
  {

console.log(this.currencyToHtml.nativeElement.innerHTML);

  let toHtml = this.currencyToHtml.nativeElement.innerHTML;
  let fromHtml = this.currencyFromHtml.nativeElement.innerHTML;

  this.currencyToHtml.nativeElement.innerHTML = fromHtml;
  this.currencyFromHtml.nativeElement.innerHTML = toHtml;
  }

This does the switch but I lose all the values on the page and the select elements dont work anymore.


Solution

  • You could define an ng-template for each inner content, and insert each one in an ng-container with ngTemplateOutlet. When appropriate, you would swap the templates of the two containers. The idea is shown in this stackblitz.

    In your case, it would be something like this:

    <ng-template #currencyFrom>
      <ion-row>
        currencyFrom stuff here...
      </ion-row>
    </ng-template>
    <ng-template #currencyTo>
      <ion-row>
        currencyTo stuff here...
      </ion-row>
    </ng-template>
    <div>
      <ng-container [ngTemplateOutlet]="topTemplate"></ng-container>
    </div>
    <div>
      <ng-container [ngTemplateOutlet]="bottomTemplate"></ng-container>
    </div>
    

    with the component code:

    @ViewChild("currencyFrom") currencyFrom: TemplateRef<any>;
    @ViewChild("currencyTo") currencyTo: TemplateRef<any>;
    
    topTemplate: TemplateRef<any>;
    bottomTemplate: TemplateRef<any>;
    
    ngOnInit(): void {
      this.topTemplate = this.currencyFrom;
      this.bottomTemplate = this.currencyTo;
    }
    
    switchToAndFromCurrency(): void {
      let temp = this.topTemplate;
      this.topTemplate = this.bottomTemplate;
      this.bottomTemplate = temp;
    }