Angular 2 Ngrx 商店:更新数组
Angular 2 Ngrx Store: Updating Array
我目前有一个基于 ngrx/store 架构的 angular2 CRM 应用程序。我有用于更新、列出和添加客户的 CRUD 功能,工作正常,并且都使用 ngrx/effects.
绑定到 Firebase 数据库
为了使智能组件与商店保持同步,我使用了以下代码。再次一切正常,但我不确定如何更新表单中的数组 - 请参见下面的代码:-
这是我使用商店中的数据更新表单的地方
if (this.customer) {
this.customerForm.get('name').setValue(this.customer.name);
this.customerForm.get('overview').setValue(this.customer.overview);
this.customerForm.get('imagePath').setValue(this.customer.imagePath);
console.log("ngOnChanges", this.customer);
}
我想对地址表单做类似的事情,但不确定如何执行上述操作以填充表单中的数组对象。欢迎任何想法/建议
当我登录下面的控制台时,我实际上有地址详细信息,只需要知道如何将它们放入表单对象中即可。
Object
addresses: Array(2)
0:Object
1:Object
length:2
imagePath:"http://www.bytes.co.uk/application/themes/bytes/img/bytes-technology-group.svg"
name:"Bytes Technology Group Emirates LLC"
overview:"Kronos Re-Seller for the Middle East and GCC Countries and Egypt"
parentId:"-KcRRsAGRYASXNU3gO_F"
完整的客户详细信息智能组件
import { Component, OnInit, Input, Output, EventEmitter, ChangeDetectionStrategy, OnChanges } from '@angular/core';
import { FormBuilder, FormGroup, FormArray, Validators, FormControl } from "@angular/forms";
// Mojito Models
import { CustomerModel } from '../../models/customer-model';
import { AddressType } from '../../../shared/models/address.model';
@Component({
selector: 'mj-customer-detail',
templateUrl: './customer-detail.component.html',
styleUrls: ['./customer-detail.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CustomerDetailComponent implements OnInit, OnChanges {
@Input() customer: CustomerModel;
@Output() updatedCustomer: EventEmitter<CustomerModel> = new EventEmitter<CustomerModel>();
@Output() showView: EventEmitter<string> = new EventEmitter<string>();
@Output() customerId: EventEmitter<string> = new EventEmitter<string>();
customerForm: FormGroup;
addressForm: FormGroup;
addressTypes: AddressType[] = [
{ "id": 1, "name": "Work" },
{ "id": 2, "name": "Home" },
]
private newAddressGroup() {
return new FormGroup({
type: new FormControl,
street: new FormControl,
city: new FormControl,
country: new FormControl,
postalCode: new FormControl
})
}
get addresses() {
return (this.addressForm.get('addressGroups') as FormArray).controls;
}
constructor(private fb: FormBuilder) {
this.customerForm = this.fb.group({
name: [null, Validators.required],
overview: [null, Validators.required],
imagePath: [null, Validators.required],
});
this.addressForm = this.fb.group({
addressGroups: this.fb.array([])
});
}
ngOnChanges() {
if (this.customer) {
this.customerForm.get('name').setValue(this.customer.name);
this.customerForm.get('overview').setValue(this.customer.overview);
this.customerForm.get('imagePath').setValue(this.customer.imagePath);
console.log("ngOnChanges", this.customer);
}
}
ngOnInit() {
}
onAddAddressGroup() {
const fa = this.addressForm.controls["addressGroups"] as FormArray;
fa.push(this.newAddressGroup());
}
onSaveAddress() {
const addresses = this.addressForm.controls["addressGroups"].value;
this.customer.addresses = addresses;
this.onSaveCustomer();
}
onSaveCustomer() {
this.customer = Object.assign(this.customer, this.customerForm.value);
this.updatedCustomer.emit(this.customer);
this.showView.emit('list');
}
onDeleteCustomer() {
this.customerId.emit(this.customer.$key);
this.showView.emit('list');
}
goBack() {
this.showView.emit('list');
}
}
您可以通过迭代 customer.addresses
中的数据来设置值,顺便说一句,您可以缩短值的设置,而不是单独设置每个值,您可以缩短它,例如:
ngOnChanges() {
if (this.customer) {
// set values in customerForm
this.customerForm.setValue({
name: this.customer.name,
overview: this.customer.overview;
imagePath: this.customer.imagePath
})
// set controls to addressForm, formArray
let control = <FormArray>this.addressForm.get('addressGroups');
this.customer.addresses.forEach(x => {
control.push(this.fb.group({...}))
})
}
}
请注意 this.fb.group({...})
我不确定您那里有什么表单控件,因此您需要附加它们...类似于:
this.fb.group({myFormControlName1: x.myProperty1, myFormControlName2: x.myProperty2....})
我目前有一个基于 ngrx/store 架构的 angular2 CRM 应用程序。我有用于更新、列出和添加客户的 CRUD 功能,工作正常,并且都使用 ngrx/effects.
绑定到 Firebase 数据库为了使智能组件与商店保持同步,我使用了以下代码。再次一切正常,但我不确定如何更新表单中的数组 - 请参见下面的代码:-
这是我使用商店中的数据更新表单的地方
if (this.customer) {
this.customerForm.get('name').setValue(this.customer.name);
this.customerForm.get('overview').setValue(this.customer.overview);
this.customerForm.get('imagePath').setValue(this.customer.imagePath);
console.log("ngOnChanges", this.customer);
}
我想对地址表单做类似的事情,但不确定如何执行上述操作以填充表单中的数组对象。欢迎任何想法/建议
当我登录下面的控制台时,我实际上有地址详细信息,只需要知道如何将它们放入表单对象中即可。
Object
addresses: Array(2)
0:Object
1:Object
length:2
imagePath:"http://www.bytes.co.uk/application/themes/bytes/img/bytes-technology-group.svg"
name:"Bytes Technology Group Emirates LLC"
overview:"Kronos Re-Seller for the Middle East and GCC Countries and Egypt"
parentId:"-KcRRsAGRYASXNU3gO_F"
完整的客户详细信息智能组件
import { Component, OnInit, Input, Output, EventEmitter, ChangeDetectionStrategy, OnChanges } from '@angular/core';
import { FormBuilder, FormGroup, FormArray, Validators, FormControl } from "@angular/forms";
// Mojito Models
import { CustomerModel } from '../../models/customer-model';
import { AddressType } from '../../../shared/models/address.model';
@Component({
selector: 'mj-customer-detail',
templateUrl: './customer-detail.component.html',
styleUrls: ['./customer-detail.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CustomerDetailComponent implements OnInit, OnChanges {
@Input() customer: CustomerModel;
@Output() updatedCustomer: EventEmitter<CustomerModel> = new EventEmitter<CustomerModel>();
@Output() showView: EventEmitter<string> = new EventEmitter<string>();
@Output() customerId: EventEmitter<string> = new EventEmitter<string>();
customerForm: FormGroup;
addressForm: FormGroup;
addressTypes: AddressType[] = [
{ "id": 1, "name": "Work" },
{ "id": 2, "name": "Home" },
]
private newAddressGroup() {
return new FormGroup({
type: new FormControl,
street: new FormControl,
city: new FormControl,
country: new FormControl,
postalCode: new FormControl
})
}
get addresses() {
return (this.addressForm.get('addressGroups') as FormArray).controls;
}
constructor(private fb: FormBuilder) {
this.customerForm = this.fb.group({
name: [null, Validators.required],
overview: [null, Validators.required],
imagePath: [null, Validators.required],
});
this.addressForm = this.fb.group({
addressGroups: this.fb.array([])
});
}
ngOnChanges() {
if (this.customer) {
this.customerForm.get('name').setValue(this.customer.name);
this.customerForm.get('overview').setValue(this.customer.overview);
this.customerForm.get('imagePath').setValue(this.customer.imagePath);
console.log("ngOnChanges", this.customer);
}
}
ngOnInit() {
}
onAddAddressGroup() {
const fa = this.addressForm.controls["addressGroups"] as FormArray;
fa.push(this.newAddressGroup());
}
onSaveAddress() {
const addresses = this.addressForm.controls["addressGroups"].value;
this.customer.addresses = addresses;
this.onSaveCustomer();
}
onSaveCustomer() {
this.customer = Object.assign(this.customer, this.customerForm.value);
this.updatedCustomer.emit(this.customer);
this.showView.emit('list');
}
onDeleteCustomer() {
this.customerId.emit(this.customer.$key);
this.showView.emit('list');
}
goBack() {
this.showView.emit('list');
}
}
您可以通过迭代 customer.addresses
中的数据来设置值,顺便说一句,您可以缩短值的设置,而不是单独设置每个值,您可以缩短它,例如:
ngOnChanges() {
if (this.customer) {
// set values in customerForm
this.customerForm.setValue({
name: this.customer.name,
overview: this.customer.overview;
imagePath: this.customer.imagePath
})
// set controls to addressForm, formArray
let control = <FormArray>this.addressForm.get('addressGroups');
this.customer.addresses.forEach(x => {
control.push(this.fb.group({...}))
})
}
}
请注意 this.fb.group({...})
我不确定您那里有什么表单控件,因此您需要附加它们...类似于:
this.fb.group({myFormControlName1: x.myProperty1, myFormControlName2: x.myProperty2....})