这是我的component.page.ts文件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-domestic',
templateUrl: './domestic.page.html',
styleUrls: ['./domestic.page.scss'],
})
export class DomesticPage implements OnInit {
opchoice: string;
constructor() { }
ngOnInit() {
this.opchoice = "From";
console.log("OnInit opchoice? " + this.opchoice);
}
onFromToChange (ev: any) {
console.log("OnFromToChange");
this.opchoice = ev.detail.value;
console.log("opchoice? >" + this.opchoice + "<");
}
}
和HTML文件。
<ion-content>
<ion-segment value="From" (ionChange)="onFromToChange($event)">
<ion-segment-button value="From">Pickup From</ion-segment-button>
<ion-segment-button value="To">Deliver To</ion-segment-button>
</ion-segment>
<div *ngIf="opchoice=='From'; then ShowFrom else ShowTo">
<ng-template #ShowFrom class="ion-text-wrap ion-no-padding ion-no-margin">
<ion-item>Domestic - Ship From</ion-item>
</ng-template>
<ng-template #ShowTo class="ion-text-wrap ion-no-padding ion-no-margin">
<ion-item>Domestic - Ship To</ion-item>
</ng-template>
</div>
</ion-content>
但是,无论我单击“ Pickup From”或“ Deliver To”选项卡/按钮,页面都不会显示任何内容(为空)。
我是否对ng-template或ngIf做错了或不了解?如您所见,我正在打印opchoice变量的值,并且对于每种类型的点击都可以正确打印。我尝试将ngIf放入离子项目,离子网格和许多其他组合中,但是没有用。
我正在将Ionic 5与Angular 9配合使用。感谢您的帮助。
答案 0 :(得分:1)
正如我之前所说,您必须将ng-templates置于同一级别。对我来说,最干净的解决方案是:
<div class="ion-text-wrap ion-no-padding ion-no-margin">
<div *ngIf="opchoice=='From'; else ShowTo">
<ion-item>Domestic - Ship From</ion-item>
</div>
<ng-template #ShowTo>
<ion-item>Domestic - Ship To</ion-item>c
</ng-template>
</div>