将几个硬编码数组传入(单击)角度2

时间:2017-02-22 17:23:26

标签: angular

我有一个按钮,它是几个硬编码数组。我没有使用*ngFor

<button type="submit" (click)="search(checkBox[0],checkBox[1],checkBox[2],checkBox[3],checkBox[4],checkBox[5])" class="btn btn-default btn-md left-button">Search</button>

在我的组件中,它只会检查checkbox[0],但不会检查其他任何内容。

public search(ssn,userId,lastName,office,role) {

        if(ssn.checked == true){
            console.log("What is checked: "+ssn.name+" input: "+this.ssn);
            this.user = this._searchService.getUserBySSN(this.ssn);
        }
        else if(userId.checked == true){
            console.log("What is checked: "+userId.name+" input: "+this.userId);
            this.user = this._searchService.getUserById(this.userId);
        }
        else if(lastName.checked == true){
            console.log("What is checked: "+lastName.name+" input: "+this.lastName);
            this.user = this._searchService.getUserByLastName(this.lastName);
        }
        else if(office.checked == true){
            console.log("What is checked: "+office.name+" input: "+this.office);
            this.user = this._searchService.getUserByOffice(this.office);
        }
        else if(role.checked == true){
            console.log("What is checked: "+role.name+" input: "+this.role);
            this.user = this._searchService.getUserByRole(this.role);
        }

    }

html我有6个复选框,如下所示:

<div class="row">
                    <div class="col-md-12 box-content right">
                        <div class="input-group">
        <span class="input-group-addon">
          <input type="checkbox" name="lastName" (change)="checkBox[1].checked=!checkBox[1].checked">
        </span>
                            <span class="input-group-addon">
          <label>{{checkBox[1].label}}</label>
        </span>
                            <input type="text" [(ngModel)]="lastName" name="lastNameTest" class="form-control" placeholder="">
                        </div>
                    </div>
                </div>

1 个答案:

答案 0 :(得分:0)

不是一次传入一个元素,而是最好传入数组对象并在那里进行检查。

试试这个:

<button type="submit" (click)="search(checkBox)" class="btn btn-default btn-md left-button">Search</button>

然后您的搜索方法变为:

public search(checkBoxArray: []){
    //Assign local variables here
    //Such as let ssn = checkBoxArray[0];
}

如果checkBox可能是具有键/值对的对象,则更容易访问。所有这一切都说,如果你传入那个数组,你可以在整个数组中调试整个数组,看看在那个时间点实际定义了什么。