Angular 2/4如何使用Angular打字稿传递/捕获单选按钮值 [英] Angular 2/4 How to pass/capture radio button values using Angular typescript

查看:48
本文介绍了Angular 2/4如何使用Angular打字稿传递/捕获单选按钮值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Angular 2/4和Typescript的新手.我想获取为Phone,电子邮件和地址选择的单选按钮值,并将选定的值传递到Typescript页面. client.info.ts

I am new to Angular 2/4 and typescript.. I want to get the radio button values selected for the Phone, email and address and pass the selected values to typescript page. client.info.ts

如何从html页面的打字稿中单击单选按钮的值.我在下面包含了我的打字稿和html代码. 当前这不起作用

How do I go about getting the values of the radio button clicked in typescript from html page. I have included my typescript and html code below. Currently this is not working

有人可以帮忙吗

这是我的代码

client-info.component.html(从JSOn文件中提取数据)

client-info.component.html(data pulled from JSOn file)

   <div class="section-container">
      <!--<div class="section-subtitle"> Client Information </div> -->
    <div class="message-info">
       <span>Choose a Primary Phone Number</span>
    </div>         
      <div class="section-content">
                 <div fxLayout="column" fxLayout.gt-xs="row"  class="row">                           


                             <div  *ngFor="let a of clientsArray; let i = index;" >

                                <div class="group" fxLayout="row" fxLayoutAlign="center center">
                                <!--<div fxFlex="100" class="label"><input type="radio"  name="phone"  checked value="{{a.homePhone}}"/> Home:</div>-->
                                <!--<div fxFlex="60" class="data">{{loanModel.getSsn()}}</div>-->
                                <div fxFlex="100" class="data"><input type="radio"  name="phone"  [(ngModel)]="a.homePhone" checked [value]="{{a.homePhone}}"/> {{a.homePhone}} (Home)</div>
                                </div>
                                 <div class="group" fxLayout="row" fxLayoutAlign="center center">
                                <!--<div fxFlex="100" class="label"><input type="radio"  name="phone" value="{{a.workPhone}}"/> Work:</div>-->
                                <!--<div fxFlex="60" class="data">{{loanModel.getSsn()}}</div>-->
                                 <div fxFlex="100" class="data"><input type="radio"  name="phone" [(ngModel)]="a.workPhone" [value]="{{a.workPhone}}"/> {{a.workPhone}} (Work)</div>
                                </div>
                                <div class="group" fxLayout="row" fxLayoutAlign="center center">
                                <!--<div fxFlex="100" class="label"><input type="radio"  name="phone" value="{{a.mobilePhone}}"/> Mobile:</div>-->
                                <!--<div fxFlex="60" class="data">{{loanModel.getSsn()}}</div>-->
                                 <div fxFlex="100" class="data"><input type="radio"  name="phone" [(ngModel)]="a.mobilePhone" [value]="{{a.mobilePhone}}"/> {{a.mobilePhone}} (Mobile)</div>
                                </div>
                                <!--<div fxHide.gt-sm class="divider"></div>-->
                            </div>  


                  </div>     
      </div>
   </div>      

   <!---->


     <!--Email -->

   <div class="section-container">
      <!--<div class="section-subtitle"> Client Information </div> -->
    <div class="message-info">
       <span>Email Address:</span>
    </div>         
      <div class="section-content">
                 <div fxLayout="column" fxLayout.gt-xs="row"  class="row">                           

                             <div  *ngFor="let a of clientsArray; let i = index;" >

                                <div class="group" fxLayout="row" fxLayoutAlign="center center">
                                <!--<div fxFlex="100" class="label">Address 1:</div>-->
                                <!--<div fxFlex="60" class="data">{{loanModel.getSsn()}}</div>-->
                                <div fxFlex="100" class="data"><input type="radio"  name="email"   [(ngModel)]="a.email1" checked [value]="{{a.email1}}"/>{{a.email1}}</div>
                                </div>
                                 <div class="group" fxLayout="row" fxLayoutAlign="center center">
                                <!--<div fxFlex="100" class="label">Address 2:</div>-->
                                <!--<div fxFlex="60" class="data">{{loanModel.getSsn()}}</div>-->
                                 <div fxFlex="100" class="data"><input type="radio"  name="email"  [(ngModel)]="a.email12" [value]="{{a.email2}}"/>{{a.email2}}</div>
                                </div>
                                <div class="group" fxLayout="row" fxLayoutAlign="center center">
                                <!--<div fxFlex="100" class="label">Address 3:</div>-->
                                <!--<div fxFlex="60" class="data">{{loanModel.getSsn()}}</div>-->
                                 <div fxFlex="100" class="data"><input type="radio"  name="email"  [(ngModel)]="a.email13" [value]="{{a.email3}}"/>{{a.email3}}</div>
                                </div>
                                <!--<div fxHide.gt-sm class="divider"></div>-->
                            </div>  


                  </div>     
      </div>
   </div>      

   <!---->



     <!--Address-->

   <div class="section-container">
      <!--<div class="section-subtitle"> Client Information </div> -->
    <div class="message-info">
       <span>Address:</span>
    </div>         
      <div class="section-content">
                 <div fxLayout="column" fxLayout.gt-xs="row"  class="row">                           

                             <div  *ngFor="let a of clientsArray; let i = index;" >

                                <div class="group" fxLayout="row" fxLayoutAlign="center center">
                                <!--<div fxFlex="100" class="label">Address 1:</div>-->
                                <!--<div fxFlex="60" class="data">{{loanModel.getSsn()}}</div>-->
                                <div fxFlex="100" class="data"><input type="radio"  name="address"  [(ngModel)]="a.address1" [value]="{{a.address1}}"/>{{a.address1}}</div>
                                </div>
                                 <div class="group" fxLayout="row" fxLayoutAlign="center center">
                                <!--<div fxFlex="100" class="label">Address 2:</div>-->
                                <!--<div fxFlex="60" class="data">{{loanModel.getSsn()}}</div>-->
                                 <div fxFlex="100" class="data"><input type="radio"  name="address" [(ngModel)]="a.address2" [value]="{{a.address3}}"/>{{a.address2}}</div>
                                </div>
                                <div class="group" fxLayout="row" fxLayoutAlign="center center">
                                <!--<div fxFlex="100" class="label">Address 3:</div>-->
                                <!--<div fxFlex="60" class="data">{{loanModel.getSsn()}}</div>-->
                                 <div fxFlex="100" class="data"><input type="radio"  name="address" [(ngModel)]="a.address3" checked [value]="{{a.address3}}"/>{{a.address3}}</div>
                                </div>
                                <!--<div fxHide.gt-sm class="divider"></div>-->
                            </div>  


                  </div>     
      </div>
   </div>      

   <!---->

    <!--  <div class-"btn-b"><input type="button" id="sendClientInfobtn" value=""><input type="button" id="Closebtn" value="Cancel"> </div> -->

   <!---->

client.info.component.ts

client.info.component.ts

 import { Component, OnInit }              from '@angular/core';
    import { Input }                          from '@angular/core';

    import { CoreService }                    from 'app/core/service/core.service';
    import { FaService }                      from '../../service/fa.service';
    import { ClientAccountModel } from "app/fa/model/client-account.model";
    import { ClientModel } from "app/fa/model/client.model";

    @Component({
      selector: 'fa-edit-client-info',
      templateUrl: './fa-edit-client-info.component.html',
      styleUrls: ['./fa-edit-client-info.component.css', '../fa.css']
    })
    export class FaEditClientInfoListComponent implements OnInit {
      @Input() clientId: string = "";
      // public clientAccountsArray: ClientAccountModel[];
      // public totalAccounts: number = 0;


      public popUpTitle = "Loan Particiapnts";
      public clientsArray: ClientModel[] = [];
      public openedIndex: number = -1;
      public totalClients: number = 0;



      constructor( 
        private coreService: CoreService,
        private faService: FaService
      ) { 
      }

      ngOnInit() {
        console.log("cliendid " +this.clientId);
        this.initData();

/*************/
        this.client = {
    workPhone: this.client.workPhone,
    homePhone: this.client.homePhone
  };
/***************/
      }

      initData(): void {
        let isEligible:boolean = true;
        this.faService.getLoanParticpantDetailsAlt(this.clientId)
          .subscribe(
            successModel => {
            this.clientsArray = successModel;
              this.totalClients = this.clientsArray.length;
            },
            error => {
              this.onError(error);
            }
          );
      }



      doSelect(): void {
        this.coreService.closeModal("");
      }

      onError(error): void {
        console.log("ERROR!: " + error);
      }

    }

推荐答案

我在我的项目中正在这样做

I'm doing like this in my projects

<input type="radio"  name="address" [(ngModel)]="radioData" [value]="a.workPhone"/>
<input type="radio"  name="address" [(ngModel)]="radioData" [value]="a.workPhone"/>
<input type="radio"  name="address" [(ngModel)]="radioData" [value]="a.workPhone"/>
<input type="radio"  name="address" [(ngModel)]="radioData" [value]="a.workPhone"/>

执行此操作ngModel将根据您的单选按钮的输入值而改变

by doing this ngModel will be change when u click the radio button according to its input value

client.info.ts我建议您遵循角度规则来命名这样的组件client-info.component.ts

client.info.ts I suggest you to follow angular rules to naming component like this client-info.component.ts

在您的组件类中,您只需delcare radioData

In your component class u simply delcare radioData

export class ClientInfoComponent implements OnInit {
    public radioData: any; <--- your radio data will be updated when on click

    ngOnInit() {
      // initialize your radio with value
       this.radioData = 1;
    }
}

这篇关于Angular 2/4如何使用Angular打字稿传递/捕获单选按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆