如何防止“错误”输入绑定更新(Angular 2+)?

时间:2018-11-25 12:14:33

标签: angular angular-components two-way-binding

假设我有两个组成部分。 function copyCells(){ var thisSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var SourceSheet = thisSpreadsheet.getSheetByName("test"); var SourceRange = thisSpreadsheet.getRange("C2:C102"); var SourceValues = SourceRange.getValues(); var destinationSheet = thisSpreadsheet.getSheetByName("test"); var destinationRange = destinationSheet.getRange("D2:D102"); var destinationValues = destinationRange.getValues(); for (var i = 0; i < SourceValues.length; i++) destinationValues[i][0] = parseFloat(destinationValues[i][0]) + parseFloat(SourceValues[i][0]) destinationRange.setValues(destinationValues); } ParentComponentChildComponent具有ParentComponent属性,并与age进行双向绑定。

ParentComponent

ChildComponent

ChildComponent

import { Component } from "@angular/core"

@Component({
    selector: "app-parent",
    template: "<app-child [(age)]="age"></app-child>"
})
export class ParentComponent {
    age = 25
}

每当import { Component, Input, Output, EventEmitter } from "@angular/core" @Component({ selector: "app-child", template: "<p>{{ age }}</p>" }) export class ChildComponent { @Input() age = 0 @Output() ageChange = new EventEmitter() } age的值发生变化时,Angular会将更改传播到ParentComponent。并且当ChildComponent发出ChildComponent事件时,更改将同步回到ageChange。当后者发生时,ParentComponent会检测到其属性已更改,并将更改传播给子级。但是孩子已经有了新的价值!

这似乎效率低下。有没有办法防止这种“假”更新?我是否缺少某些东西,或者这是Angular的工作方式?

0 个答案:

没有答案