我创建了一个angular2组件来托管Label以及输入和验证消息。模板看起来像这样:
<div class="form-group" [ngClass]="{'has-error':!formcontrol.valid && formcontrol.touched}">
<label class="col-sm-2 control-label" >{{label}}</label>
<div class="col-sm-6">
<input class="form-control" type="text" [formControl]="formcontrol">
<div *ngIf="formcontrol.hasError('required') && formcontrol.touched" class="alert alert-danger">muss ausgefüllt werden.</div>
<div *ngIf="formcontrol.hasError('minlength') && formcontrol.touched" class="alert alert-danger">diese Eingabe ist zu kurz.</div>
<div *ngIf="formcontrol.hasError('maxlength') && formcontrol.touched" class="alert alert-danger">diese Eingabe ist zu lang.</div>
</div>
</div>
我的component.ts:
import { Component, Input } from "@angular/core";
@Component({
selector: "bs-ctrl-string",
template: require("./bs-ctrl-string.component.html"),
})
export class BsCtrlStringComponent {
@Input() label: string;
@Input() formcontrol: any
}
我使用其他组件中的这个组件:
<bs-ctrl-string [label]="'first name'" [formcontrol]="form.controls['firstname']"></bs-ctrl-string>
<bs-ctrl-string [label]="'last name'" [formcontrol]="form.controls['lastname']"></bs-ctrl-string>
我的问题是:对于端到端测试,我需要访问输入:
browser.FindElement(By.Id("firstname")).SendKeys("Sam");
问题是:如何从托管组件设置组件中输入字段的ID?它需要是这样的:
<bs-ctrl-string inputid="firstname" [label]="'first name'" [formcontrol]="form.controls['firstname']"></bs-ctrl-string>
<bs-ctrl-string inputid="lastname" [label]="'last name'" [formcontrol]="form.controls['lastname']"></bs-ctrl-string>