【问题标题】:Angular 5 it overwrites always the selected date with the current dateAngular 5 它总是用当前日期覆盖选定的日期
【发布时间】:2019-04-14 23:58:27
【问题描述】:

我有一个输入字段,其中包含一个日期,但问题是当我不想选择一个不是今天日期的日期时,每次都会采用当前日期并保存在后端。 我希望在输入字段中选择例如 15.11.2018 将被保存。 在控制台日志中的保存方法中,它告诉我正确的事情,我只认为是减速器中的问题

这是我的代码

<app-input-field label="{{'general.#date'|translate}}">
                    <input type="date" step="1" [ngModel]="newCosts.choosenDate| date:'yyyy-MM-dd'" (ngModelChange)="newCosts.choosenDate = $event">
                </app-input-field>

save() {
    this.newCosts.choosenDate = new Date(this.newCosts.choosenDate)
    this.store.dispatch(new UpsertCostsAction({
        costs: { ...this.newCosts } 

    }))
    this.newCosts = emptyCosts()
}

export interface Costs {
    id: string     
    choosenDate: Date
}
export function emptyCosts(): Costs {
    return {
        id: "",
        choosenDate: new Date(),

    }
}

减速器

function toCostsViewModel(costs: Costs): Costs {
    costs.choosenDate = new Date(costs.choosenDate)

    return costs
}

function upsertCosts(state: ValueItemDatabaseState, action: UpsertCostsInternalAction): ValueItemDatabaseState {
    action.payload.forEach(cost => {
        state.costs = state.costs.set(cost.id, toCostsViewModel(cost))
    })
    return {
        ...state
    }
}

【问题讨论】:

    标签: html angular typescript reducers


    【解决方案1】:

    在你的保存方法中

    save() {
        this.newCosts.choosenDate = new Date(this.newCosts.choosenDate)
        this.store.dispatch(new UpsertCostsAction({
            costs: { ...this.newCosts } 
    
        }))
        this.newCosts = emptyCosts()
    }
    

    在你运行this.newCosts = emptyCosts() 和emptycosts 并覆盖你的choosenDate: new Date() 基本上创建一个新的现在日期。

    你能确认吗?

    【讨论】:

    • 我已经找到了解决方案,在后端它覆盖了一个新的日期
    猜你喜欢
    • 2016-08-23
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    相关资源
    最近更新 更多