【发布时间】:2017-04-06 10:41:51
【问题描述】:
我正在尝试复制这个:http://plnkr.co/edit/OB2YTMJyeY3h9FOaztak?p=preview (这个 plunker 是有效的示例,我想获得相同的结果,但我的代码不起作用) p>
================================================ ====================
我有这个简单的双向绑定,我正在尝试更新一个字符串属性,例如父级和子级的字符串属性
问题:当点击“从父级更新”时,两个绑定都会更新。但是当点击“从孩子更新”时,只有孩子更新!
这看起来很简单,我做错了什么?
(注意:我使用 angular CLI 来运行项目)
================================================ ====================
父组件:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-my-dad',
templateUrl: './my-dad.component.html',
styleUrls: ['./my-dad.component.css']
})
export class MyDadComponent {
parentModel: string;
constructor() {}
updateModel(){
this.parentModel += ' parent';
}
}
父模板
<h2>Parent: {{ parentModel }} </h2>
<button (click)="updateModel()"> update from parent </button>
<app-my-child [(model)]="parentModel"></app-my-child>
子组件
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-my-child',
templateUrl: './my-child.component.html',
styleUrls: ['./my-child.component.css']
})
export class MyChildComponent {
@Input() model: string;
constructor() { }
updateModel(){
this.model += ' child';
}
}
子模板:
<h2>Child: {{ model }} </h2>
<button (click)="updateModel()"> update from child </button>
【问题讨论】:
标签: angular data-binding