我在使用formbuilder時存在角度2的問題。我遇到以下情況。使用formbuilder更改對象數組的值
我正在驗證電子郵件,如果我已經在員工數組內部清理了該字段,如果允許,則什麼也不做。我運行一個foreach來檢查這一點,我在構造函數創建的formbuild和指涉部分看起來是這樣的:
我添加與需求的目的因此。
我把一個模糊事件中輸入並執行該功能。
public verifyEmail(employee_email: string, index) {
if (employee_email === '') {
return;
}
let ocorrency = 0;
this.formOrganization.value.employees.forEach(employee => {
if (employee.email === employee_email) {
ocorrency += 1
}
});
if (ocorrency >= 2) {
this.messageNotify(false, 'email_already_exists');
this.formOrganization.get('employees')[index].patchValue({'email': ''});
}
}
對於發送2個參數的函數,第一個是,我需要測試的電子郵件,並且指的是我的工作陣列內對象的第二索引,它是在該索引,我需要零場。
如何做到這一點?
我的HTML是:
<div class="col-md-12 col-12 tabs-content" *ngIf="membersTabs">
<div class="row" formArrayName="employees">
<div class="col-12">
<button class="btn btn-new pull-left" (click)="addEmployeer()">Adicionar Membros</button>
</div>
<div class="col-12">
<div class="card card-members"
*ngFor="let employeer of formOrganization.controls.employees.controls; let index = index"
[formGroupName]="index">
<div class="card-header">
<div class="col-sm-12 col-md-8">
<strong class="text-capitalize">{{formOrganization.value.employees[index].first_name}}
{{formOrganization.value.employees[index].second_name}}</strong>
</div>
<div class="wrapper-switch col-sm-12 col-md-4">
<div class="row">
<div class="col-sm-12 col-md-6">
<button class="btn btn-new btn-transparent" *ngIf="formOrganization.value.employees[index]._id"
(click)="[changePasswordSelected(employeer.value), passwordModal.show()]">ALTERAR SENHA
</button>
</div>
<div class="col-sm-12 col-md-6">
<button class="btn btn-cancel" (click)="removeEmployeer(index)">Remover</button>
</div>
</div>
</div>
</div>
<div class="card-block">
<div class="row">
<div class="col-lg-4 col-md-6 col-sm-12 form-group">
<label class="col-md-10 form-control-label">Primeiro Nome</label>
<div class="col-md-12">
<input class="form-control " name="first_name" type="text"
formControlName="first_name">
</div>
</div>
<div class="col-lg-4 col-md-6 col-sm-12 form-group">
<label class="col-md-10 form-control-label">Sobrenome</label>
<div class="col-md-12">
<input class="form-control " name="second_name" type="text"
formControlName="second_name">
</div>
</div>
<div class="col-lg-4 col-md-6 col-sm-12 form-group">
<label class="col-md-10 form-control-label">Cargo</label>
<div class="col-md-12">
<input class="form-control " name="office" type="text" formControlName="office">
</div>
</div>
<div class="col-lg-2 col-md-6 col-sm-12 form-group">
<label class="col-md-10 form-control-label">Acesso</label>
<div class="col-md-12">
<select class="form-control modal-input" id="access" name="access" formControlName="access">
<option value="">Selecione a opção</option>
<option *ngFor="let item of accesss"
[selected]="item.name == employeer.value.access.name"
[ngValue]="item">{{item.name}}
</option>
</select>
</div>
</div>
<div class="col-lg-2 col-md-1 col-sm-12 form-group">
<label class="col-md-10 form-control-label">Status</label>
<div class="col-md-12">
<label class="switch switch-text switch-pill switch-primary">
<input type="checkbox" class="switch-input " formControlName="status">
<span class="switch-label" data-on="On" data-off="Off"></span>
<span class="switch-handle"></span>
</label>
</div>
</div>
<div class="col-lg-4 col-md-6 col-sm-12 form-group">
<label class="col-md-10 form-control-label">Email</label>
<div class="col-md-12">
<input class="form-control " name="email" type="text" formControlName="email"
(blur)="verifyEmail(formOrganization.value.employees[index].email, index)">
</div>
</div>
<div class="col-lg-4 col-md-5 col-sm-12 form-group"
*ngIf="!formOrganization.value.employees[index]._id">
<label class="col-md-10 form-control-label">Senha</label>
<div class="col-md-12">
<input class="form-control " name="password" type="password"
formControlName="password">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
我的形式從formBuilder:
你想達到和/或在什麼不是很清楚問題是。你無法獲得索引作爲第二個參數嗎? –
同意w/@ P.Moloney - 如果你從你的組件模板中調用'(blur)=「verifyEmail()」',你應該可以從你的html傳入索引, i = index'在你的'* ngFor'語句中。但是,如果這是問題,我們必須看到您的模板HTML。 –
我的問題正在發生,因爲我想清除我的字段,我需要在字段中設置一個值,模型很容易這樣做:myModel =「我想要的字符串」但我使用formBuilder,除此之外,我可以有0,1或infinitor僱員。 –