【发布时间】:2018-10-03 16:51:05
【问题描述】:
我正在尝试在角度组件中实现两种方式的数据绑定。目前它处于父子模式。
parent.component.html
<child [(title)]="title"></child>
<span style="color: red">This is parent component {{title}}</span>
parent.component.ts
title = 'app';
child.component.html
<span style="color: blue">This is child component {{title}}</span>
child.component.ts
@Input() title: any;
@Output() pushTitle = new EventEmitter();
constructor() { }
ngOnInit() {
this.title = 'new title';
this.pushTitle.emit(this.title);
}
当我从子组件更改标题时,标题也应该在父组件上实现。另外,我不确定为什么父代码会无缘无故地循环。我在 html 中添加了文本只是为了测试它是否在两个组件中都更新了,但它只在子组件中更新,而不是在父组件中更新。我来自 angularjs 背景,两种方式的数据绑定在其中无缝工作。我只是对自己做错了什么感到困惑(我知道这是一个菜鸟问题)。
【问题讨论】:
-
简单的方法是从
@Output() pushTitle = new EventEmitter();更新到@Output() titleChange = new EventEmitter();
标签: javascript angular