【发布时间】:2017-11-08 17:14:45
【问题描述】:
我有一个 Angular2 项目(IntelliJ IDEA),我根据this 示例使用双向绑定:
child.component.ts:
counterValue = 0;
@Output() counterChange = new EventEmitter();
@Input()
get counter() {
return this.counterValue;
}
set counter(val) {
this.counterValue = val;
this.counterChange.emit(this.counterValue);
}
decrement() {
this.counter--;
}
increment() {
this.counter++;
}
child.component.html:
<button (click)="decrement()">-</button>
<span>{{counter}}</span>
<button (click)="increment()">+</button>
parent.component.html:
<child-component [(counter)]="co"></child-component>
co: {{co}}
parent.component.ts:
co = 4;
代码按预期工作 - 除非我更改一些变量名称(两种方式:使用重构以及手动 - 查找所有引用)。
我可以更改 counterValue 的名称(甚至通过整个代码更改变量类型),但只要重构 counterChange(据我所知,出现 2 次)或 counter(...)(4 in child.component.ts,child.component.html 中的 2,parent.html 中的 1)与父级的输出绑定停止工作。然而,子组件总是按预期工作;只是父组件的 co 变量不再更新。
我重启了 IntelliJ 和 npm,清除了 IntelliJ 和浏览器的缓存:没有帮助。
通过将名称改回示例中的名称,输出绑定再次起作用。
我现在的想法已经不多了,并且有一种忽略一些基本事物的可怕感觉......
【问题讨论】:
-
请告诉我们更改后的变量名称是什么样的?我怀疑知道错误可能是什么;)
-
我尝试了从
temp到onTest到visible和onVisibilityChange的几十种变体(主要目的:模仿对话的可见性行为)。在我凭直觉尝试以下方法之前,没有任何效果:发射器必须是后缀为Change的 getter 和 setter 函数名。所以cat123作为getter 和setter 与cat123Change一起工作...... -
是的,这就是为什么我要求更改变量名,输出需要是输入变量名+后缀
Change -
好的。所以这确实是基本的(而且 - 老实说 - 有点烦人)。有了这些信息,我发现一些其他线程指向相同的错误,例如这个:Angular 2 two way data binding not working。非常感谢,伙计们!你让我免于发疯。
标签: angular intellij-idea