简体   繁体   English

使用Angular4 / bootstrap获取切换按钮的值

[英]Fetch the value of the toggle button using Angular4/bootstrap

How do i get the value of the radio button which is clicked using Angularjs 4 如何获取使用Angularjs 4单击的单选按钮的值

I tried using ngModel but not able to get the value. 我尝试使用ngModel但无法获取该值。

@Component({
  selector: 'button-view',
  template: `
  <div class="btn-group" id="status" data-toggle="buttons" (click)="actionBtnClick()">
    <label class="btn btn-default btn-on btn-xs active">
    <input type="radio" [value]="1" name="status" checked="checked" [(ngModel)]="status">ON</label>
    <label class="btn btn-default btn-off btn-xs">
    <input type="radio" [value]="0" name="status" [(ngModel)]="status">OFF</label>
  </div>
  `,
  styles: [
    `
    .btn-info{
      color: #ffffff;
    }
    .btn-default.btn-on.active{background-color: #5BB75B;color: white;}
    .btn-default.btn-off.active{background-color: #DA4F49;color: white;}

`],
})
export class ButtonViewComponent implements ViewCell, OnInit {
  status: any = 1;
  constructor() {}
  actionBtnClick() {
    console.log('inside'+ status);
    }
  }
}

I did some small changes of your code here is an example from your code working, 我对您的代码做了一些小的更改,这是您的代码正常工作的示例,

status: any = 1;
actionBtnClick() {
      console.log('inside', this.status);
}   

In typescript if you want to access declared variable as public then you have to use this keyword . 在typescript中,如果要访问声明的变量为public,则必须使用此关键字

在此处输入图片说明

let try this once and let me know. 让我们尝试一次,让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM