Nulra
Nulra

Reputation: 547

Ionic2 can't get ion-segment ngmodel value

I have a html like below:

<ion-content>
<ion-segment [(ngModel)]="authorize">
<ion-segment-button value="register">
   Register
</ion-segment-button>
<ion-segment-button value="login">
   Login
</ion-segment-button>
</ion-segment>
<div [ngSwitch]="authorize">
<ion-list *ngSwitchCase="'register'">
  <p>register</p>
</ion-list>
<ion-list *ngSwitchCase="'login'">
  <p>login</p>
</ion-list>    
</div>
</ion-content>

And I want to selecting one of the segment in ts below:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { NavParams } from 'ionic-angular';

@Component({
 selector: 'page-login',
 templateUrl: 'login.html'
})
export class LoginPage {
  constructor(public navCtrl: NavController, private navParams: NavParams) {
    let sel = navParams.get('sel');
    if(sel == 'register')
      authorize: string = "register";
else if(sel == 'login')
  authorize: string = "login";
  }
}

But when I compile have error below:

Property 'authorize' does not exist on type 'LoginPage'.

Does anyone know how to solve this problem?

Upvotes: 2

Views: 843

Answers (1)

Aravind
Aravind

Reputation: 41571

Place the authorize at class level,

export class LoginPage {

authorize:string;

  constructor(public navCtrl: NavController, private navParams: NavParams) {
    let sel = navParams.get('sel');
    if(sel == 'register'){
        this.authorize= "register";
    }else if(sel == 'login'){
        this.authorize= "login";
    }
  }
}

The mistake is because what if the variable sel does not match both register or login.

Upvotes: 1

Related Questions