Annaimahalingam
Annaimahalingam

Reputation: 252

Read the value from p-selectButton in onchange ~ primeNg,angular 2

SelectButton is used to choose single or multiple items from a list using buttons.

basedOnModeofTrack(mode){
    // currentTypeMode has value time means it show time dropdown
    console.log(mode);
    this.currentTypeMode = mode;
  }
<p-selectButton name="trackingmode" (onChange)="basedOnModeofTrack()" [options]="trackingmodes" [(ngModel)]="stolenvehicletrack.trackingmode"></p-selectButton>

Upvotes: 1

Views: 10150

Answers (2)

HD..
HD..

Reputation: 1476

Check import

import {SelectButtonModule} from 'primeng/primeng';

Two-way value binding is defined using ngModel and selectbutton requires a collection of options where each option should follow the SelectItem interface that defines label-value properties.

View:

<p-selectButton [options]="cities" [(ngModel)]="selectedCity"></p-selectButton>

JS CODE:

export class SelectButtonDemo {

    types: SelectItem[];

    selectedType: string;

    selectedTypes: string[] = ['Apartment','Studio'];

    constructor() {
        this.types = [];
        this.types.push({label: 'Apartment', value: 'Apartment'});
        this.types.push({label: 'House', value: 'House'});
        this.types.push({label: 'Studio', value: 'Studio'});
    }    
}

this.selectedType is having selected data. you can use this.selectedType to get it.

Upvotes: 0

gropapa
gropapa

Reputation: 577

you missed the event property

<p-selectButton name="trackingmode" (onChange)="basedOnModeofTrack($event)" [options]="trackingmodes" [(ngModel)]="stolenvehicletrack.trackingmode"></p-selectButton>

in your ts file

public basedOnModeofTrack(obj:any){
    console.log(obj.value);
}

Upvotes: 5

Related Questions