3

我将表单数据以状态存储在数组中。我正在接收数组,但它是嵌套形式的。我不知道如何显示它。

//查看Viewcomponent.ts

customerarray: Customer[];
ngOnInit() {
// this.customerObs = this.store.select('customerList');
this.store.select<Customer[]>('customerList').subscribe(res =>                     
    {
      this.customerarray = res;
      console.log(res);
      console.log(this.customerarray);
    });
}

//viewcomponent.html

<li *ngFor="let customer of customerarray; i as index">
      <span>{{ i + 1}}.</span>  {{customer.customer.name}}
</li>

//reducer.ts

import { Customer } from '../app/models/customer';
import { ADD_CUSTOMER } from '../app/store/action';
import * as CustomerAction from '../app/store/action';
const initialState = {
    customer: [
        new Customer('Steve', 'Yellow'),
        new Customer('RDJ', 'Red')
    ]
};

export function CustomerReducer(state = initialState, action: CustomerAction.AddCustomer) {
    console.log(state.customer);
    switch (action.type) {
        case CustomerAction.ADD_CUSTOMER:
            return {`enter code here`

            ...state,                
            customer: [...state.customer, action.payload]
            };

        default:
            return state;
    }
}
4

2 回答 2

2

我认为这是一个变化检测问题。您的组件不会在此订阅上呈现。

尝试这个 -

.ts 文件 -

customersObs:Observable<Customer[]>
constructor() {
 this.customersObs = this.store.select<Customer[]>('customerList');
}

.html 文件 -

<li *ngFor="let customer of cusomersObs | async; i as index">
      <span>{{ i + 1}}.</span>  {{customer.name}}
</li>
于 2019-06-16T13:48:18.103 回答
0

我假设你Customer的类是这样定义的 -

export class Customer {
  name: string;
  color: string;
  constructor(n: string, c: string) {
     this.name = n;
     this.color = c;
  }
}

我还假设您的选择器this.store.select<Customer[]>('customerList')customer您的initialState.

如果我是正确的,那么您应该像这样更新您的模板 -

<li *ngFor="let customer of customerarray; i as index">
      <span>{{ i + 1}}.</span>  {{customer.name}}
</li>
于 2019-06-11T15:31:17.757 回答