在Array中添加时,预算不起作用

时间:2018-02-20 10:29:53

标签: angular typescript ngrx

我有位置和预算界面,当我从对话框中添加新位置和预算时,所有位置界面都被添加但预算显示0当我重新加载页面预算时它正在运行一切正常。 我添加了一个位置并且位置已添加并显示但是当我重新加载页面时计划的预算为0然后一切正常,这怎么会发生你建议我做什么自动显示预算,而不是页面正在重新加载。

这里有代码, 这是显示位置的方法

  getPosition() {
return this.positions.sort((function (a, b) {
  if (a.name > b.name) {
    return 1;
  }})).filter(position => this.subproject.positionIds.includes(position.id)).toArray();
}

这是模板

 <tr *ngFor="let position of getPosition()">
<td>{{position.name}}</td>
                          <td>{{costTypes[position.costType]}}</td>
                          <td>{{getActualBudget(position).planned | number}}</td>
                          <td>{{currencies[position.currency]}}</td>
                          <td>{{states[position.state]}}</td>

这是位置

的界面
export interface Position {
  id: string;
     subProjectId: string;
  name: string;
 currency: string;
 state: string;
 costType: string;
 budgetIds: string[];
 }

这是预算

的界面
 export interface Budget {
 id: string;
 positionId: string;
 created: Date;
 planned: number;
 blocked: number;
 used: number;
 free: number;
 transferred: number;
 transferredToId: string;
 commentUsed: string;
 commentFree: string;

}

这是实际预算

getActualBudget(position: Position): Budget {
return this.budgets.get(position.budgetIds[0], emptyBudget());
}

这是对话框的模板。

 <app-input-field orientation="top" labelWidth="85px;" label="Planned 
  Budget *">
      <input type="number" min="1"  [(ngModel)]="newBudget.planned" 
      required style="width: 100%;">
     </app-input-field>

以下是预算的实用工具

export function emptyBudget(): Budget {
return {
id: '',
positionId: '',
created: new Date(0),
planned: 0,
blocked: 0,
used: 0,
free: 0,
transferred: 0,
transferredToId: '',
commentUsed: '',
commentFree: ''
};
}

以下是效果。

upsertBudgetEffect$ = this.actions$.ofType(UpsertBudgetAction.Type).pipe(
    withLatestFrom(this.store),
    mergeMap(([action, state]: [UpsertBudgetAction, ApplicationState]) => {
      const url = environment.backend + '/api/planning/budgets';
      return this.httpClient.put<Budget>(url, action.payload).pipe(
        mergeMap(response => {
          const actions: Action[] = [new UpsertBudgetInternalAction(response)];
          const position = state.budcon.positions.get(response.positionId);

          if (position.budgetIds.includes(response.id)) {
            position.budgetIds.push(response.id);
            actions.push(new UpsertPositionInternalAction(position));
          }

          return actions;
        }),
        catchError(noOpHandler)
      );
    })
  );

1 个答案:

答案 0 :(得分:0)

在条件下的效果如果你失败了。 改变  if (position.budgetIds.includes(response.id))

if (!position.budgetIds.includes(response.id))