Angular 2/4 How to pass/capture radio button values using Angular typescript

风格不统一 提交于 2019-12-23 05:04:45

问题


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

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

can someone help

here is my code for the

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

 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);
      }

    }

回答1:


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"/>

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

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

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;
    }
}



回答2:


either use template driven form and [(ng-model)] and to get "value" of the check Box or use Reactive form and use form-control name property to get value of the input assigned to the model.




回答3:


It is recommended to use the FormsModule or Reactive Forms for dealing with form inputs

FormsModule Approach

add this to your app.module.ts

import { FormsModule } from '@angular/forms';

@NgModule({
  imports:      [ BrowserModule, FormsModule ], // import forms module
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})

then inside your component.html

<div>
  <input type="radio" name="phone" [(ngModel)]="client.homePhone" [value]="client.homePhone"> 
  {{client.homePhone}}
</div>

<div>
  <input type="radio" name="phone" [(ngModel)]="client.workPhone" [value]="client.workPhone"> 
  {{client.workPhone}}
</div>

inside your component.ts create a model for the client object

ngOnInit() {
  this.client = {
    workPhone: '',
    homePhone: ''
  };
}

you can use the values from the object anywhere inside the component by referencing the required properties. For eg, if you want to access workPhone use this.client.workPhone.



来源:https://stackoverflow.com/questions/46177562/angular-2-4-how-to-pass-capture-radio-button-values-using-angular-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!