2017-05-07 85 views
1

我目前有一个基于ngrx/store架构的angular2 CRM应用程序。我有CRUD功能用于更新,列出和添加使用ngrx/effects的Firebase数据库工作正常的所有客户。Angular 2 Ngrx商店:更新数组

为了保持智能组件与商店同步我有以下代码。同样所有工作正常,但是我不确定如何更新形式的阵列 - 见下面的代码: -

这是我从商店

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'); 

    } 

} 

回答

2

您可以通过迭代你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({...})我不知道WH在表单控件在你那里,所以你需要添加他们...喜欢的东西:

this.fb.group({myFormControlName1: x.myProperty1, myFormControlName2: x.myProperty2....}) 
+0

'我不知道你有什么样的形式控制there'他用'newAddressGroup' :)所以,你可以更新答案 – yurzui

+0

我知道他想设置'customer.addresses'的值吗? Quote:***当我登录控制台下面我实际上有地址的详细信息只需要知道如何让他们进入表单对象。** – Alex

+0

是的,我认为你的回答是正确的。我只是暗示,他的地址组看起来像'新FormGroup({ 类型:新FormControl, 街道:新FormControl, 城市:新FormControl, 国家:新FormControl, 邮政编码:新FormControl })' – yurzui