Angular 2/4 如何使用 Angular typescript pass/capture 单选按钮值
Angular 2/4 How to pass/capture radio button values using Angular typescript
我是 Angular 2/4 和 typescript 的新手。我想获取为 Phone、电子邮件和地址选择的单选按钮值,并将所选值传递到 typescript 页面。 client.info.ts
如何从 html 页面获取打字稿中单击的单选按钮的值。我在下面包含了我的打字稿和 html 代码。
目前这不起作用
有人可以帮忙吗
这是我的代码
客户端-info.component.html(从JSOn文件中提取的数据)
<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);
}
}
要么使用模板驱动表单和 [(ng-model)] 并获取 "value" 复选框,要么使用 Reactive 表单并使用表单控件名称 属性 获取值分配给模型的输入。
我在我的项目中就是这样做的
<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
将根据其输入值单击单选按钮时更改
client.info.ts
我建议您遵循 angular 规则来命名组件,例如 client-info.component.ts
在你的组件中 class 你只是 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;
}
}
建议使用FormsModule
或响应式表单处理表单输入
FormsModule
方法
将此添加到您的 app.module.ts
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [ BrowserModule, FormsModule ], // import forms module
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
然后在你的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>
在您的 component.ts 中为 client
对象创建一个模型
ngOnInit() {
this.client = {
workPhone: '',
homePhone: ''
};
}
您可以通过引用所需的属性在组件内的任何位置使用对象的值。例如,如果您想访问 workPhone,请使用 this.client.workPhone
.
我是 Angular 2/4 和 typescript 的新手。我想获取为 Phone、电子邮件和地址选择的单选按钮值,并将所选值传递到 typescript 页面。 client.info.ts
如何从 html 页面获取打字稿中单击的单选按钮的值。我在下面包含了我的打字稿和 html 代码。 目前这不起作用
有人可以帮忙吗
这是我的代码
客户端-info.component.html(从JSOn文件中提取的数据)
<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);
}
}
要么使用模板驱动表单和 [(ng-model)] 并获取 "value" 复选框,要么使用 Reactive 表单并使用表单控件名称 属性 获取值分配给模型的输入。
我在我的项目中就是这样做的
<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
将根据其输入值单击单选按钮时更改
client.info.ts
我建议您遵循 angular 规则来命名组件,例如 client-info.component.ts
在你的组件中 class 你只是 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;
}
}
建议使用FormsModule
或响应式表单处理表单输入
FormsModule
方法
将此添加到您的 app.module.ts
import { FormsModule } from '@angular/forms';
@NgModule({
imports: [ BrowserModule, FormsModule ], // import forms module
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
然后在你的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>
在您的 component.ts 中为 client
对象创建一个模型
ngOnInit() {
this.client = {
workPhone: '',
homePhone: ''
};
}
您可以通过引用所需的属性在组件内的任何位置使用对象的值。例如,如果您想访问 workPhone,请使用 this.client.workPhone
.