Ionic 4如何在点击时更改细分

时间:2019-11-10 03:03:53

标签: ionic4

我需要知道如何通过单击按钮来更改细分?我尝试使用ngModel,但显示错误ngModel无法与离子段绑定。然后,我在app.module.ts中导入FormModule,但仍然是同样的错误。谁能告诉我点击时如何更改细分按钮?谢谢

  <ion-segment value="javascript">
      <ion-segment-button value="python">
        <ion-label>Python</ion-label>
      </ion-segment-button>
      <ion-segment-button value="javascript">
        <ion-label>Javascript</ion-label>
      </ion-segment-button>
    </ion-segment>
    <button (click)="next()></button>

1 个答案:

答案 0 :(得分:0)

您似乎错误地使用了离子段。您需要将ionChange事件添加到离子段中。请按照“离子文档”中的离子段进行操作:https://ionicframework.com/docs/api/segment

这是一个例子:

在您的HTML文件中:

<ion-segment (ionChange)="segmentChanged($event)" [(ngModel)]="segment">
  <ion-segment-button value="first">
    <ion-label>First</ion-label>
  </ion-segment-button>
  <ion-segment-button value="second">
    <ion-label>Second</ion-label>
  </ion-segment-button>
</ion-segment>

在您的打字稿中:

export class Example {
  segment = "segment value";

  constructor(public navCtrl: NavController) {}

  segmentChanged(ev: any) {
    // Add your logic here
  }
}