找到哪个单选按钮而不使用* ngFor

时间:2017-02-23 17:39:21

标签: angular

我尝试了很多不同的方法:

 <input type="radio" name="group" id="ssnRadio" [checked]="ssn" [(ngModel)]="ssnSelected">

<input type="radio" name="group" [checked]="lastName" [(ngModel)]="lastNameSelected" >

还有更多的单选按钮,但它们会像上面一样加强。它们都是独立的,因此不在*ngFor

在组件中我有以下内容:

  ssnSelected: boolean;
    userIdSelected: boolean;
    lastNameSelected: boolean;
    officeSelected: boolean;
    roleSelected: boolean;

    selectedRow: number;

    ngOnInit(){
        this.ssnSelected=false;
        this.userIdSelected=false;
        this.lastNameSelected=false;
        this.officeSelected=false;
        this.roleSelected=false;
    }

按钮在其中包含此方法:

 search() {

        if (this.ssnSelected==true) {
            console.log( this.ssn);
            this.user = this._searchService.getUserBySSN(this.ssn);
        }
        if (this.userIdSelected == true) {
            console.log(this.userId);
            this.user = this._searchService.getUserById(this.userId);
        }

所以我需要找到选择的单选按钮,这样我就可以调用正确的服务并传入一个使用双向绑定到服务的值。

1 个答案:

答案 0 :(得分:0)

您需要订阅表单更改或单个输入更改。

<input type="radio" 
    name="group" 
    id="ssnRadio" 
    [checked]="ssn" 
    [(ngModel)]="ssnSelected" 
    (change)="doSomething($event)">